@@ -496,6 +496,8 @@ func (client *NginxClient) post(path string, input interface{}) error {
496
496
"expected %v response, got %v" ,
497
497
http .StatusCreated , resp .StatusCode ))
498
498
}
499
+ // empty response Body so keepalive will work
500
+ io .Copy (ioutil .Discard , resp .Body )
499
501
500
502
return nil
501
503
}
@@ -778,3 +780,187 @@ func (client *NginxClient) getStreamUpstreams() (*StreamUpstreams, error) {
778
780
}
779
781
return & upstreams , nil
780
782
}
783
+
784
+ // GetKeyVals fetches all key/value pairs for a given zone. (http)
785
+ func (client * NginxClient ) GetKeyVals (zone string ) (map [string ]string , error ) {
786
+ return client .getKeyVals (zone , false )
787
+ }
788
+
789
+ // GetStreamKeyVals fetches all key/value pairs for a given zone. (stream)
790
+ func (client * NginxClient ) GetStreamKeyVals (zone string ) (map [string ]string , error ) {
791
+ return client .getKeyVals (zone , true )
792
+ }
793
+
794
+ func (client * NginxClient ) getKeyVals (zone string , stream bool ) (map [string ]string , error ) {
795
+ keyvals := make (map [string ]string )
796
+ base := "http"
797
+ if stream {
798
+ base = "stream"
799
+ }
800
+ err := client .get (fmt .Sprintf ("%v/keyvals/%v" , base , zone ), & keyvals )
801
+ if err != nil {
802
+ return nil , fmt .Errorf ("failed to get keyvals for zone: %v/%v err: %v" , base , zone , err )
803
+ }
804
+ return keyvals , nil
805
+ }
806
+
807
+ // GetKeyValsAll fetches all key/value pairs for all zones. (http)
808
+ func (client * NginxClient ) GetKeyValsAll () (map [string ]map [string ]string , error ) {
809
+ return client .getKeyValsAll (false )
810
+ }
811
+
812
+ // GetStreamKeyValsAll fetches all key/value pairs for all zones. (stream)
813
+ func (client * NginxClient ) GetStreamKeyValsAll () (map [string ]map [string ]string , error ) {
814
+ return client .getKeyValsAll (true )
815
+ }
816
+
817
+ func (client * NginxClient ) getKeyValsAll (stream bool ) (map [string ]map [string ]string , error ) {
818
+ keyvals := make (map [string ]map [string ]string )
819
+ base := "http"
820
+ if stream {
821
+ base = "stream"
822
+ }
823
+ err := client .get (fmt .Sprintf ("%v/keyvals" , base ), & keyvals )
824
+ if err != nil {
825
+ return nil , fmt .Errorf ("failed to get keyvals for all %v zones. err: %v" , base , err )
826
+ }
827
+ return keyvals , nil
828
+ }
829
+
830
+ // AddKeyVal adds a new key/value pair.
831
+ func (client * NginxClient ) AddKeyVal (zone string , keyval map [string ]string ) error {
832
+ return client .addKeyVal (zone , keyval , false )
833
+ }
834
+
835
+ // AddStreamKeyVal adds a new key/value pair. (stream)
836
+ func (client * NginxClient ) AddStreamKeyVal (zone string , keyval map [string ]string ) error {
837
+ return client .addKeyVal (zone , keyval , true )
838
+ }
839
+
840
+ func (client * NginxClient ) addKeyVal (zone string , keyval map [string ]string , stream bool ) error {
841
+ APIVersion := 3
842
+ base := "http"
843
+ if stream {
844
+ base = "stream"
845
+ }
846
+ path := fmt .Sprintf ("%v/keyvals" , base )
847
+ url := fmt .Sprintf ("%v/%v/%v" , client .apiEndpoint , APIVersion , path )
848
+ if zone != "" {
849
+ url = fmt .Sprintf ("%v/%v" , url , zone )
850
+ }
851
+
852
+ jsonInput , err := json .Marshal (keyval )
853
+ if err != nil {
854
+ return fmt .Errorf ("failed to marshall input: %v" , err )
855
+ }
856
+
857
+ resp , err := client .httpClient .Post (url , "application/json" , bytes .NewBuffer (jsonInput ))
858
+ if err != nil {
859
+ return fmt .Errorf ("failed to create delete request: %v" , err )
860
+ }
861
+ defer resp .Body .Close ()
862
+
863
+ if resp .StatusCode != http .StatusCreated {
864
+ return createResponseMismatchError (resp .Body ).Wrap (fmt .Sprintf (
865
+ "expected %v response, got %v" ,
866
+ http .StatusCreated , resp .StatusCode ))
867
+ }
868
+ io .Copy (ioutil .Discard , resp .Body )
869
+ return nil
870
+ }
871
+
872
+ // ModifyKeyVal modifies the value of an existing key. (http)
873
+ func (client * NginxClient ) ModifyKeyVal (zone string , keyval map [string ]string ) error {
874
+ return client .modifyKeyVal (zone , keyval , false )
875
+ }
876
+
877
+ // ModifyStreamKeyVal modifies the value of an existing key. (stream)
878
+ func (client * NginxClient ) ModifyStreamKeyVal (zone string , keyval map [string ]string ) error {
879
+ return client .modifyKeyVal (zone , keyval , true )
880
+ }
881
+
882
+ func (client * NginxClient ) modifyKeyVal (zone string , keyval map [string ]string , stream bool ) error {
883
+ APIVersion := 3
884
+ base := "http"
885
+ if stream {
886
+ base = "stream"
887
+ }
888
+ path := fmt .Sprintf ("%v/keyvals" , base )
889
+ url := fmt .Sprintf ("%v/%v/%v" , client .apiEndpoint , APIVersion , path )
890
+ if zone != "" {
891
+ url = fmt .Sprintf ("%v/%v" , url , zone )
892
+ } else {
893
+ return fmt .Errorf ("zone required" )
894
+ }
895
+
896
+ jsonInput , err := json .Marshal (keyval )
897
+ if err != nil {
898
+ return fmt .Errorf ("failed to marshall input: %v" , err )
899
+ }
900
+ req , err := http .NewRequest (http .MethodPatch , url , bytes .NewBuffer (jsonInput ))
901
+ if err != nil {
902
+ return fmt .Errorf ("failed to create a patch request: %v" , err )
903
+ }
904
+ req .Header .Set ("Content-Type" , "application/json" )
905
+
906
+ resp , err := client .httpClient .Do (req )
907
+ if err != nil {
908
+ return fmt .Errorf ("failed to do patch request: %v" , err )
909
+ }
910
+ defer resp .Body .Close ()
911
+ // We will consider ONLY 204 as success
912
+ if resp .StatusCode != http .StatusNoContent {
913
+ return createResponseMismatchError (resp .Body ).Wrap (fmt .Sprintf (
914
+ "expected %v response, got %v" ,
915
+ http .StatusNoContent , resp .StatusCode ))
916
+ }
917
+ io .Copy (ioutil .Discard , resp .Body )
918
+ return nil
919
+ }
920
+
921
+ // DeleteKey deletes the key/value pair for a given key. (http)
922
+ func (client * NginxClient ) DeleteKey (zone string , key string ) error {
923
+ return client .deleteKey (zone , key , false )
924
+ }
925
+
926
+ // DeleteStreamKey deletes the key/value pair for a given key. (stream)
927
+ func (client * NginxClient ) DeleteStreamKey (zone string , key string ) error {
928
+ return client .deleteKey (zone , key , true )
929
+ }
930
+
931
+ func (client * NginxClient ) deleteKey (zone string , key string , stream bool ) error {
932
+ APIVersion := 3
933
+ base := "http"
934
+ if stream {
935
+ base = "stream"
936
+ }
937
+ path := fmt .Sprintf ("%v/keyvals" , base )
938
+ url := fmt .Sprintf ("%v/%v/%v" , client .apiEndpoint , APIVersion , path )
939
+ if zone != "" {
940
+ url = fmt .Sprintf ("%v/%v" , url , zone )
941
+ } else {
942
+ return fmt .Errorf ("zone required" )
943
+ }
944
+
945
+ req , err := http .NewRequest (http .MethodDelete , url , nil )
946
+ if err != nil {
947
+ return fmt .Errorf ("failed to create a delete request: %v" , err )
948
+ }
949
+ req .Header .Set ("Content-Type" , "application/json" )
950
+
951
+ resp , err := client .httpClient .Do (req )
952
+ if err != nil {
953
+ return fmt .Errorf ("failed to do delete request: %v" , err )
954
+ }
955
+ defer resp .Body .Close ()
956
+
957
+ // Expect status 204
958
+ if resp .StatusCode != http .StatusNoContent {
959
+ return createResponseMismatchError (resp .Body ).Wrap (fmt .Sprintf (
960
+ "expected %v response, got %v" ,
961
+ http .StatusNoContent , resp .StatusCode ))
962
+ }
963
+ io .Copy (ioutil .Discard , resp .Body )
964
+ return nil
965
+
966
+ }
0 commit comments