github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/api/cloudcontroller/ccv3/process.go (about) 1 package ccv3 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 8 "code.cloudfoundry.org/cli/api/cloudcontroller" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal" 11 "code.cloudfoundry.org/cli/types" 12 ) 13 14 type Process struct { 15 GUID string `json:"guid"` 16 Type string `json:"type"` 17 HealthCheckType string `json:"-"` 18 HealthCheckEndpoint string `json:"-"` 19 HealthCheckInvocationTimeout int `json:"-"` 20 Instances types.NullInt `json:"instances,omitempty"` 21 MemoryInMB types.NullUint64 `json:"memory_in_mb,omitempty"` 22 DiskInMB types.NullUint64 `json:"disk_in_mb,omitempty"` 23 } 24 25 func (p Process) MarshalJSON() ([]byte, error) { 26 type healthCheck struct { 27 Type string `json:"type"` 28 Data struct { 29 Endpoint interface{} `json:"endpoint"` 30 InvocationTimeout int `json:"invocation_timeout,omitempty"` 31 } `json:"data"` 32 } 33 34 var ccProcess struct { 35 Instances json.Number `json:"instances,omitempty"` 36 MemoryInMB json.Number `json:"memory_in_mb,omitempty"` 37 DiskInMB json.Number `json:"disk_in_mb,omitempty"` 38 39 HealthCheck *healthCheck `json:"health_check,omitempty"` 40 } 41 42 if p.Instances.IsSet { 43 ccProcess.Instances = json.Number(fmt.Sprint(p.Instances.Value)) 44 } 45 if p.MemoryInMB.IsSet { 46 ccProcess.MemoryInMB = json.Number(fmt.Sprint(p.MemoryInMB.Value)) 47 } 48 if p.DiskInMB.IsSet { 49 ccProcess.DiskInMB = json.Number(fmt.Sprint(p.DiskInMB.Value)) 50 } 51 52 if p.HealthCheckType != "" || p.HealthCheckEndpoint != "" || p.HealthCheckInvocationTimeout != 0 { 53 ccProcess.HealthCheck = new(healthCheck) 54 ccProcess.HealthCheck.Type = p.HealthCheckType 55 ccProcess.HealthCheck.Data.InvocationTimeout = p.HealthCheckInvocationTimeout 56 if p.HealthCheckEndpoint != "" { 57 ccProcess.HealthCheck.Data.Endpoint = p.HealthCheckEndpoint 58 } 59 } 60 61 return json.Marshal(ccProcess) 62 } 63 64 func (p *Process) UnmarshalJSON(data []byte) error { 65 type rawProcess Process 66 var ccProcess struct { 67 *rawProcess 68 69 HealthCheck struct { 70 Type string `json:"type"` 71 Data struct { 72 Endpoint string `json:"endpoint"` 73 InvocationTimeout int `json:"invocation_timeout"` 74 } `json:"data"` 75 } `json:"health_check"` 76 } 77 78 ccProcess.rawProcess = (*rawProcess)(p) 79 err := cloudcontroller.DecodeJSON(data, &ccProcess) 80 if err != nil { 81 return err 82 } 83 84 p.HealthCheckEndpoint = ccProcess.HealthCheck.Data.Endpoint 85 p.HealthCheckType = ccProcess.HealthCheck.Type 86 p.HealthCheckInvocationTimeout = ccProcess.HealthCheck.Data.InvocationTimeout 87 88 return nil 89 } 90 91 // CreateApplicationProcessScale updates process instances count, memory or disk 92 func (client *Client) CreateApplicationProcessScale(appGUID string, process Process) (Process, Warnings, error) { 93 body, err := json.Marshal(process) 94 if err != nil { 95 return Process{}, nil, err 96 } 97 98 request, err := client.newHTTPRequest(requestOptions{ 99 RequestName: internal.PostApplicationProcessActionScaleRequest, 100 Body: bytes.NewReader(body), 101 URIParams: internal.Params{"app_guid": appGUID, "type": process.Type}, 102 }) 103 if err != nil { 104 return Process{}, nil, err 105 } 106 107 var responseProcess Process 108 response := cloudcontroller.Response{ 109 Result: &responseProcess, 110 } 111 err = client.connection.Make(request, &response) 112 return responseProcess, response.Warnings, err 113 } 114 115 // GetApplicationProcessByType returns application process of specified type 116 func (client *Client) GetApplicationProcessByType(appGUID string, processType string) (Process, Warnings, error) { 117 request, err := client.newHTTPRequest(requestOptions{ 118 RequestName: internal.GetApplicationProcessRequest, 119 URIParams: map[string]string{ 120 "app_guid": appGUID, 121 "type": processType, 122 }, 123 }) 124 if err != nil { 125 return Process{}, nil, err 126 } 127 var process Process 128 response := cloudcontroller.Response{ 129 Result: &process, 130 } 131 132 err = client.connection.Make(request, &response) 133 return process, response.Warnings, err 134 } 135 136 // GetApplicationProcesses lists processes for a given app 137 func (client *Client) GetApplicationProcesses(appGUID string) ([]Process, Warnings, error) { 138 request, err := client.newHTTPRequest(requestOptions{ 139 RequestName: internal.GetApplicationProcessesRequest, 140 URIParams: map[string]string{"app_guid": appGUID}, 141 }) 142 if err != nil { 143 return nil, nil, err 144 } 145 146 var fullProcessesList []Process 147 warnings, err := client.paginate(request, Process{}, func(item interface{}) error { 148 if process, ok := item.(Process); ok { 149 fullProcessesList = append(fullProcessesList, process) 150 } else { 151 return ccerror.UnknownObjectInListError{ 152 Expected: Process{}, 153 Unexpected: item, 154 } 155 } 156 return nil 157 }) 158 159 return fullProcessesList, warnings, err 160 } 161 162 // PatchApplicationProcessHealthCheck updates application health check type 163 func (client *Client) PatchApplicationProcessHealthCheck(processGUID string, processHealthCheckType string, processHealthCheckEndpoint string, processHealthCheckInvocationTimeout int) (Process, Warnings, error) { 164 body, err := json.Marshal(Process{ 165 HealthCheckType: processHealthCheckType, 166 HealthCheckEndpoint: processHealthCheckEndpoint, 167 HealthCheckInvocationTimeout: processHealthCheckInvocationTimeout, 168 }) 169 if err != nil { 170 return Process{}, nil, err 171 } 172 173 request, err := client.newHTTPRequest(requestOptions{ 174 RequestName: internal.PatchProcessRequest, 175 Body: bytes.NewReader(body), 176 URIParams: internal.Params{"process_guid": processGUID}, 177 }) 178 if err != nil { 179 return Process{}, nil, err 180 } 181 182 var responseProcess Process 183 response := cloudcontroller.Response{ 184 Result: &responseProcess, 185 } 186 err = client.connection.Make(request, &response) 187 return responseProcess, response.Warnings, err 188 }