github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/resources/process_resource.go (about) 1 package resources 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 "code.cloudfoundry.org/cli/api/cloudcontroller" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 9 "code.cloudfoundry.org/cli/types" 10 ) 11 12 type Process struct { 13 GUID string 14 Type string 15 // Command is the process start command. Note: This value will be obfuscated when obtained from listing. 16 Command types.FilteredString 17 HealthCheckType constant.HealthCheckType 18 HealthCheckEndpoint string 19 HealthCheckInvocationTimeout int64 20 HealthCheckTimeout int64 21 Instances types.NullInt 22 MemoryInMB types.NullUint64 23 DiskInMB types.NullUint64 24 LogRateLimitInBPS types.NullInt 25 AppGUID string 26 } 27 28 func (p Process) MarshalJSON() ([]byte, error) { 29 var ccProcess marshalProcess 30 31 marshalCommand(p, &ccProcess) 32 marshalInstances(p, &ccProcess) 33 marshalMemory(p, &ccProcess) 34 marshalDisk(p, &ccProcess) 35 marshalLogRateLimit(p, &ccProcess) 36 marshalHealthCheck(p, &ccProcess) 37 38 return json.Marshal(ccProcess) 39 } 40 41 func (p *Process) UnmarshalJSON(data []byte) error { 42 var ccProcess struct { 43 Command types.FilteredString `json:"command"` 44 DiskInMB types.NullUint64 `json:"disk_in_mb"` 45 GUID string `json:"guid"` 46 Instances types.NullInt `json:"instances"` 47 MemoryInMB types.NullUint64 `json:"memory_in_mb"` 48 LogRateLimitInBPS types.NullInt `json:"log_rate_limit_in_bytes_per_second"` 49 Type string `json:"type"` 50 Relationships Relationships `json:"relationships"` 51 52 HealthCheck struct { 53 Type constant.HealthCheckType `json:"type"` 54 Data struct { 55 Endpoint string `json:"endpoint"` 56 InvocationTimeout int64 `json:"invocation_timeout"` 57 Timeout int64 `json:"timeout"` 58 } `json:"data"` 59 } `json:"health_check"` 60 } 61 62 err := cloudcontroller.DecodeJSON(data, &ccProcess) 63 if err != nil { 64 return err 65 } 66 67 p.Command = ccProcess.Command 68 p.DiskInMB = ccProcess.DiskInMB 69 p.GUID = ccProcess.GUID 70 p.HealthCheckEndpoint = ccProcess.HealthCheck.Data.Endpoint 71 p.HealthCheckInvocationTimeout = ccProcess.HealthCheck.Data.InvocationTimeout 72 p.HealthCheckTimeout = ccProcess.HealthCheck.Data.Timeout 73 p.HealthCheckType = ccProcess.HealthCheck.Type 74 p.Instances = ccProcess.Instances 75 p.MemoryInMB = ccProcess.MemoryInMB 76 p.LogRateLimitInBPS = ccProcess.LogRateLimitInBPS 77 p.Type = ccProcess.Type 78 p.AppGUID = ccProcess.Relationships[constant.RelationshipTypeApplication].GUID 79 80 return nil 81 } 82 83 type healthCheck struct { 84 Type constant.HealthCheckType `json:"type,omitempty"` 85 Data struct { 86 Endpoint interface{} `json:"endpoint,omitempty"` 87 InvocationTimeout int64 `json:"invocation_timeout,omitempty"` 88 Timeout int64 `json:"timeout,omitempty"` 89 } `json:"data"` 90 } 91 92 type marshalProcess struct { 93 Command interface{} `json:"command,omitempty"` 94 Instances json.Number `json:"instances,omitempty"` 95 MemoryInMB json.Number `json:"memory_in_mb,omitempty"` 96 DiskInMB json.Number `json:"disk_in_mb,omitempty"` 97 LogRateLimitInBPS json.Number `json:"log_rate_limit_in_bytes_per_second,omitempty"` 98 99 HealthCheck *healthCheck `json:"health_check,omitempty"` 100 } 101 102 func marshalCommand(p Process, ccProcess *marshalProcess) { 103 if p.Command.IsSet { 104 ccProcess.Command = &p.Command 105 } 106 } 107 108 func marshalDisk(p Process, ccProcess *marshalProcess) { 109 if p.DiskInMB.IsSet { 110 ccProcess.DiskInMB = json.Number(fmt.Sprint(p.DiskInMB.Value)) 111 } 112 } 113 114 func marshalHealthCheck(p Process, ccProcess *marshalProcess) { 115 if p.HealthCheckType != "" || p.HealthCheckEndpoint != "" || p.HealthCheckInvocationTimeout != 0 || p.HealthCheckTimeout != 0 { 116 ccProcess.HealthCheck = new(healthCheck) 117 ccProcess.HealthCheck.Type = p.HealthCheckType 118 ccProcess.HealthCheck.Data.InvocationTimeout = p.HealthCheckInvocationTimeout 119 ccProcess.HealthCheck.Data.Timeout = p.HealthCheckTimeout 120 if p.HealthCheckEndpoint != "" { 121 ccProcess.HealthCheck.Data.Endpoint = p.HealthCheckEndpoint 122 } 123 } 124 } 125 126 func marshalInstances(p Process, ccProcess *marshalProcess) { 127 if p.Instances.IsSet { 128 ccProcess.Instances = json.Number(fmt.Sprint(p.Instances.Value)) 129 } 130 } 131 132 func marshalMemory(p Process, ccProcess *marshalProcess) { 133 if p.MemoryInMB.IsSet { 134 ccProcess.MemoryInMB = json.Number(fmt.Sprint(p.MemoryInMB.Value)) 135 } 136 } 137 138 func marshalLogRateLimit(p Process, ccProcess *marshalProcess) { 139 if p.LogRateLimitInBPS.IsSet { 140 ccProcess.LogRateLimitInBPS = json.Number(fmt.Sprint(p.LogRateLimitInBPS.Value)) 141 } 142 }