@@ -22,7 +22,9 @@ import (
2222
2323 "github.com/spf13/cast"
2424 "github.com/spf13/cobra"
25+ "gopkg.in/yaml.v2"
2526
27+ "github.com/liquidweb/liquidweb-cli/instance"
2628 "github.com/liquidweb/liquidweb-cli/types/api"
2729 "github.com/liquidweb/liquidweb-cli/types/cmd"
2830 "github.com/liquidweb/liquidweb-cli/validate"
@@ -50,10 +52,33 @@ such as '80:80,443:443'.
5052
5153For example, to set these values for the service with source port 443, the flag could look like this:
5254
53- --health-check 443_failure_threshold=12,443_http_body_match=hello,443_http_path=/status,443_http_response_codes=200:201:202,443_http_use_tls=true,443_interval=10,443_protocol=tcp ,443_timeout=99
55+ --health-check 443_failure_threshold=12,443_http_body_match=hello,443_http_path=/status,443_http_response_codes=200:201:202,443_http_use_tls=true,443_interval=10,443_protocol=http ,443_timeout=99
5456
5557Notice the leading '443_' before the parameter name. To create a health check for service 80 as well, follow the same pattern, but
5658replacing '443_' with '80_'.`
59+ var networkLoadBalancerServicesHealthCheckFileHelp = `--health-check-file value should be the path to a yaml file containing the health check(s) to apply for each service(s). Here is
60+ an example of how that file should look:
61+
62+ 443:
63+ protocol: http
64+ timeout: 5
65+ interval: 10
66+ http_use_tls: true
67+ http_response_codes: 200-202,404
68+ http_path: /status-443
69+ http_body_match:
70+ failure_threshold: 3
71+ 80:
72+ protocol: http
73+ timeout: 10
74+ interval: 20
75+ http_use_tls: false
76+ http_response_codes: 200-202,404
77+ http_path: /status-80
78+ http_body_match:
79+ failure_threshold: 3
80+
81+ It is an error to provide both --health-check and --health-check-file flags.`
5782
5883var networkLoadBalancerUpdateCmd = & cobra.Command {
5984 Use : "update" ,
@@ -64,11 +89,15 @@ A Load Balancer allows you to distribute traffic to multiple endpoints.
6489
6590%s
6691
92+ %s
93+
6794To remove a health check from a service, simply call update for the service(s) omitting their --health-check entries. For example,
6895this would remove any set health checks for services 443:443,80:80 (as well as remove any other services entirely):
6996
7097network load-balancer update --uniq_id ABC123 --services 443:443,80:80
71- ` , networkLoadBalancerServicesHealthChecksHelp ),
98+
99+ Similarly to remove a health check when using --health-check-file, simply remove the health check from the file.
100+ ` , networkLoadBalancerServicesHealthChecksHelp , networkLoadBalancerServicesHealthCheckFileHelp ),
72101 Run : func (cmd * cobra.Command , args []string ) {
73102 uniqIdFlag , _ := cmd .Flags ().GetString ("uniq_id" )
74103 nameFlag , _ := cmd .Flags ().GetString ("name" )
@@ -80,6 +109,7 @@ network load-balancer update --uniq_id ABC123 --services 443:443,80:80
80109 sslIntermediateCertFlag , _ := cmd .Flags ().GetString ("ssl-intermediate-certificate" )
81110 enableSslIncludesFlag , _ := cmd .Flags ().GetBool ("enable-ssl-includes" )
82111 disableSslIncludesFlag , _ := cmd .Flags ().GetBool ("disable-ssl-includes" )
112+ healthCheckFileFlag , _ := cmd .Flags ().GetString ("health-check-file" )
83113
84114 if enableSslTerminationFlag && disableSslTerminationFlag {
85115 lwCliInst .Die (fmt .Errorf ("can't both enable and disable ssl termination" ))
@@ -97,6 +127,9 @@ network load-balancer update --uniq_id ABC123 --services 443:443,80:80
97127 lwCliInst .Die (fmt .Errorf ("when using --ssl-certificate or --ssl-private-key --enable-ssl-termination must be passed" ))
98128 }
99129 }
130+ if len (healthChecksMapUpdate ) > 0 && healthCheckFileFlag != "" {
131+ lwCliInst .Die (fmt .Errorf ("cannot pass conflicting flags --health-check and --health-check-file" ))
132+ }
100133
101134 validateFields := map [interface {}]interface {}{
102135 uniqIdFlag : "UniqId" ,
@@ -178,12 +211,42 @@ network load-balancer update --uniq_id ABC123 --services 443:443,80:80
178211 // services
179212 if len (networkLoadBalancerUpdateServicesCmd ) > 0 {
180213 var servicesToBalance []map [string ]interface {}
181- // a service is permitted to have one health check
182- healthChecks , err := cmdTypes.LoadBalancerHealthCheck {HealthCheck : healthChecksMapUpdate }.Transform ()
183- if err != nil {
184- lwCliInst .Die (err )
214+ // a service is permitted to have one health check.
215+
216+ var healthChecks map [string ]map [string ]interface {}
217+
218+ // health check, command line flags.
219+ if len (healthChecksMapUpdate ) > 0 {
220+ healthChecksFromCmdLine , err := cmdTypes.LoadBalancerHealthCheckCmdLine {HealthCheck : healthChecksMapUpdate }.Transform ()
221+ if err != nil {
222+ lwCliInst .Die (err )
223+ }
224+ healthChecks = healthChecksFromCmdLine
225+ } else if healthCheckFileFlag != "" {
226+ // health check, yaml file
227+ contents , err := ioutil .ReadFile (healthCheckFileFlag )
228+ if err != nil {
229+ lwCliInst .Die (fmt .Errorf ("error reading given --health-check-file [%s]: %s" , healthCheckFileFlag , err ))
230+ }
231+ if err = yaml .Unmarshal (contents , & healthChecks ); err != nil {
232+ lwCliInst .Die (fmt .Errorf ("error yaml decoding [%s] (see help for an example of the file); %s" , healthCheckFileFlag , err ))
233+ }
234+ }
235+
236+ // validate
237+ for _ , healthCheck := range healthChecks {
238+ var obj apiTypes.NetworkLoadBalancerDetailsServiceHealthCheck
239+ if err := instance .CastFieldTypes (healthCheck , & obj ); err != nil {
240+ lwCliInst .Die (fmt .Errorf (
241+ "failed casting --health-check-file [%s] to expected structure (see help for an example of the file): %s" ,
242+ healthCheckFileFlag , err ))
243+ }
244+ if err := obj .Validate (); err != nil {
245+ lwCliInst .Die (err )
246+ }
185247 }
186248
249+ // build services api argument
187250 for _ , pair := range networkLoadBalancerUpdateServicesCmd {
188251 err := validate .Validate (map [interface {}]interface {}{pair : "NetworkPortPair" })
189252 if err != nil {
@@ -206,7 +269,6 @@ network load-balancer update --uniq_id ABC123 --services 443:443,80:80
206269
207270 servicesToBalance = append (servicesToBalance , serviceToBalance )
208271 }
209-
210272 apiArgs ["services" ] = servicesToBalance
211273 }
212274
@@ -250,7 +312,10 @@ func init() {
250312 []string {}, "source/destination port pairs (such as 80:80) separated by ',' to balance via the Load Balancer" )
251313
252314 networkLoadBalancerUpdateCmd .Flags ().StringToStringVar (& healthChecksMapUpdate , "health-check" , nil ,
253- "Health check defintions for the service matching source port" )
315+ "Health check defintions for the service matching source port. Should not be combined with --health-check." )
316+
317+ networkLoadBalancerUpdateCmd .Flags ().String ("health-check-file" , "" ,
318+ "A file containing valid yaml describing the LoadBalancer health checks to add for the service(s). Should not be combined with --health-check." )
254319
255320 networkLoadBalancerUpdateCmd .MarkFlagRequired ("uniq_id" )
256321}
0 commit comments