Skip to content

Commit fbc5498

Browse files
#714 added ability to edit a LB protocol with SSL id. Also addressed #715
1 parent 653979d commit fbc5498

4 files changed

Lines changed: 67 additions & 47 deletions

File tree

plugin/commands/loadbal/protocol_edit.go

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type ProtocolEditCommand struct {
2727
Sticky string
2828
ClientTimeout int
2929
ServerTimeout int
30+
SslId int
3031
}
3132

3233
func NewProtocolEditCommand(sl *metadata.SoftlayerCommand) *ProtocolEditCommand {
@@ -37,23 +38,30 @@ func NewProtocolEditCommand(sl *metadata.SoftlayerCommand) *ProtocolEditCommand
3738
cobraCmd := &cobra.Command{
3839
Use: "protocol-edit",
3940
Short: T("Edit load balancer protocol"),
40-
Long: T("${COMMAND_NAME} sl loadbal protocol-edit (--id LOADBAL_ID) (--protocol-uuid PROTOCOL_UUID) [--front-protocol PROTOCOL] [back-protocol PROTOCOL] [--front-port PORT] [--back-port PORT] [-m, --method METHOD] [-c, --connections CONNECTIONS] [--sticky cookie | source-ip] [--client-timeout SECONDS] [--server-timeout SECONDS]"),
41-
Args: metadata.NoArgs,
41+
Long: T(`Use '${COMMAND_NAME} sl loadbal detail' to find the --protocol-uuid values for a loadbalancer
42+
Example:
43+
${COMMAND_NAME} sl loadbal protocol-add --id 1115129 --protocol-uuid 8ec8911a-c32d-4678-89fe-979f182c822f --ssl-id 123
44+
This command changes the SSL certificate
45+
`),
46+
Args: metadata.NoArgs,
4247
RunE: func(cmd *cobra.Command, args []string) error {
4348
return thisCmd.Run(args)
4449
},
4550
}
46-
cobraCmd.Flags().IntVar(&thisCmd.Id, "id", 0, T("ID for the load balancer [required]"))
51+
cobraCmd.Flags().IntVar(&thisCmd.Id, "id", -1, T("ID for the load balancer [required]"))
4752
cobraCmd.Flags().StringVar(&thisCmd.ProtocolUuid, "protocol-uuid", "", T("UUID of the protocol you want to edit."))
48-
cobraCmd.Flags().StringVar(&thisCmd.FrontProtocol, "front-protocol", "HTTP", T("Protocol type to use for incoming connections: [HTTP|HTTPS|TCP]. Default: HTTP"))
53+
cobraCmd.Flags().StringVar(&thisCmd.FrontProtocol, "front-protocol", "", T("Protocol type to use for incoming connections: [HTTP|HTTPS|TCP]. Default: HTTP"))
4954
cobraCmd.Flags().StringVar(&thisCmd.BackProtocol, "back-protocol", "", T("Protocol type to use when connecting to backend servers: [HTTP|HTTPS|TCP]. Defaults to whatever --front-protocol is"))
50-
cobraCmd.Flags().IntVar(&thisCmd.FrontPort, "front-port", 80, T("Internet side port"))
51-
cobraCmd.Flags().IntVar(&thisCmd.BackPort, "back-port", 80, T("Private side port"))
52-
cobraCmd.Flags().StringVarP(&thisCmd.Method, "method", "m", "ROUNDROBIN", T("Balancing Method: [ROUNDROBIN|LEASTCONNECTION|WEIGHTED_RR]"))
53-
cobraCmd.Flags().IntVarP(&thisCmd.Connections, "connections", "c", 0, T("Maximum number of connections to allow"))
55+
cobraCmd.Flags().IntVar(&thisCmd.FrontPort, "front-port", -1, T("Internet side port"))
56+
cobraCmd.Flags().IntVar(&thisCmd.BackPort, "back-port", -1, T("Private side port"))
57+
cobraCmd.Flags().StringVarP(&thisCmd.Method, "method", "m", "", T("Balancing Method: [ROUNDROBIN|LEASTCONNECTION|WEIGHTED_RR]"))
58+
cobraCmd.Flags().IntVarP(&thisCmd.Connections, "connections", "c", -1, T("Maximum number of connections to allow"))
5459
cobraCmd.Flags().StringVar(&thisCmd.Sticky, "sticky", "", T("Use 'cookie' or 'source-ip' to stick"))
55-
cobraCmd.Flags().IntVar(&thisCmd.ClientTimeout, "client-timeout", 0, T("Client side timeout setting, in seconds"))
56-
cobraCmd.Flags().IntVar(&thisCmd.ServerTimeout, "server-timeout", 0, T("Server side timeout setting, in seconds"))
60+
cobraCmd.Flags().IntVar(&thisCmd.ClientTimeout, "client-timeout", -1, T("Client side timeout setting, in seconds"))
61+
cobraCmd.Flags().IntVar(&thisCmd.ServerTimeout, "server-timeout", -1, T("Server side timeout setting, in seconds"))
62+
cobraCmd.Flags().IntVar(&thisCmd.SslId, "ssl-id", -1, T("Identifier of the SSL certificate to attach to this protocol. Only valid for HTTPS."))
63+
cobraCmd.MarkFlagRequired("id")
64+
cobraCmd.MarkFlagRequired("protocol-uuid")
5765
thisCmd.Command = cobraCmd
5866
return thisCmd
5967
}
@@ -62,7 +70,7 @@ func (cmd *ProtocolEditCommand) Run(args []string) error {
6270
protocolConfiguration := datatypes.Network_LBaaS_LoadBalancerProtocolConfiguration{}
6371

6472
loadbalID := cmd.Id
65-
if loadbalID == 0 {
73+
if loadbalID == -1 {
6674
return errors.NewMissingInputError("--id")
6775
}
6876

@@ -71,45 +79,37 @@ func (cmd *ProtocolEditCommand) Run(args []string) error {
7179
return errors.New(T("Failed to get load balancer: {{.ERR}}.", map[string]interface{}{"ERR": err.Error()}))
7280
}
7381

74-
protoUUID := cmd.ProtocolUuid
75-
if protoUUID == "" {
82+
if cmd.ProtocolUuid == "" {
7683
return errors.NewMissingInputError("--protocol-uuid")
7784
}
78-
protocolConfiguration.ListenerUuid = &protoUUID
85+
protocolConfiguration.ListenerUuid = &cmd.ProtocolUuid
7986

8087
if cmd.FrontProtocol != "" {
81-
frontProtocol := cmd.FrontProtocol
82-
protocolConfiguration.FrontendProtocol = &frontProtocol
88+
protocolConfiguration.FrontendProtocol = &cmd.FrontProtocol
8389
}
8490

8591
if cmd.BackProtocol != "" {
86-
backProtocol := cmd.BackProtocol
87-
protocolConfiguration.BackendProtocol = &backProtocol
92+
protocolConfiguration.BackendProtocol = &cmd.BackProtocol
8893
}
8994

90-
if cmd.FrontPort != 0 {
91-
frontPort := cmd.FrontPort
92-
protocolConfiguration.FrontendPort = &frontPort
95+
if cmd.FrontPort != -1 {
96+
protocolConfiguration.FrontendPort = &cmd.FrontPort
9397
}
9498

95-
if cmd.BackPort != 0 {
96-
backPort := cmd.BackPort
97-
protocolConfiguration.BackendPort = &backPort
99+
if cmd.BackPort != -1 {
100+
protocolConfiguration.BackendPort = &cmd.BackPort
98101
}
99102

100103
if cmd.Method != "" {
101-
method := cmd.Method
102-
protocolConfiguration.LoadBalancingMethod = &method
104+
protocolConfiguration.LoadBalancingMethod = &cmd.Method
103105
}
104106

105-
if cmd.ClientTimeout != 0 {
106-
cTimeout := cmd.ClientTimeout
107-
protocolConfiguration.ClientTimeout = &cTimeout
107+
if cmd.ClientTimeout != -1 {
108+
protocolConfiguration.ClientTimeout = &cmd.ClientTimeout
108109
}
109110

110-
if cmd.ServerTimeout != 0 {
111-
sTimeout := cmd.ServerTimeout
112-
protocolConfiguration.ServerTimeout = &sTimeout
111+
if cmd.ServerTimeout != -1 {
112+
protocolConfiguration.ServerTimeout = &cmd.ServerTimeout
113113
}
114114

115115
var sessionType string
@@ -123,12 +123,17 @@ func (cmd *ProtocolEditCommand) Run(args []string) error {
123123
return errors.NewInvalidUsageError(T("Value of option '--sticky' should be cookie or source-ip"))
124124
}
125125

126-
if cmd.Connections != 0 {
127-
connections := cmd.Connections
128-
protocolConfiguration.MaxConn = &connections
126+
if cmd.Connections != -1 {
127+
protocolConfiguration.MaxConn = &cmd.Connections
129128
}
130129

131-
_, err = cmd.LoadBalancerManager.AddLoadBalancerListener(&loadbalancerUUID, []datatypes.Network_LBaaS_LoadBalancerProtocolConfiguration{protocolConfiguration})
130+
if cmd.SslId != 0 {
131+
protocolConfiguration.TlsCertificateId = &cmd.SslId
132+
}
133+
134+
_, err = cmd.LoadBalancerManager.AddLoadBalancerListener(
135+
&loadbalancerUUID, []datatypes.Network_LBaaS_LoadBalancerProtocolConfiguration{protocolConfiguration},
136+
)
132137
if err != nil {
133138
return errors.New(T("Failed to edit protocol: {{.Error}}.\n", map[string]interface{}{"Error": err.Error()}))
134139
}

plugin/commands/loadbal/protocol_edit_test.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package loadbal_test
22

33
import (
44
"errors"
5-
5+
"fmt"
66
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal"
77
. "github.com/onsi/ginkgo/v2"
88
. "github.com/onsi/gomega"
@@ -41,11 +41,11 @@ var _ = Describe("LoadBal_protocol-edit_Test", func() {
4141
It("Error No Id", func() {
4242
err := testhelpers.RunCobraCommand(cliCommand.Command)
4343
Expect(err).To(HaveOccurred())
44-
Expect(err.Error()).To(ContainSubstring("Incorrect Usage: '--id' is required"))
44+
Expect(err.Error()).To(ContainSubstring(`required flag(s) "id", "protocol-uuid" not set`))
4545
})
4646
It("Error unable to find Id", func() {
4747
fakeLBManager.GetLoadBalancerUUIDReturns("-", errors.New("SoftLayer_Exception_ApiError"))
48-
err := testhelpers.RunCobraCommand(cliCommand.Command, "--id", "12345")
48+
err := testhelpers.RunCobraCommand(cliCommand.Command, "--id", "12345", "--protocol-uuid=aaaa")
4949
Expect(err).To(HaveOccurred())
5050
Expect(err.Error()).To(ContainSubstring("Failed to get load balancer: SoftLayer_Exception_ApiError"))
5151
})
@@ -55,11 +55,14 @@ var _ = Describe("LoadBal_protocol-edit_Test", func() {
5555
It("Error no UUID", func() {
5656
err := testhelpers.RunCobraCommand(cliCommand.Command, "--id", "12345")
5757
Expect(err).To(HaveOccurred())
58-
Expect(err.Error()).To(ContainSubstring("'--protocol-uuid' is required"))
58+
Expect(err.Error()).To(ContainSubstring(`required flag(s) "protocol-uuid" not set`))
5959
})
6060
})
6161

6262
Context("Testing Options", func() {
63+
BeforeEach(func() {
64+
fakeLBManager.GetLoadBalancerUUIDReturns("aaa-bbb-111", nil)
65+
})
6366
It("with all arguments", func() {
6467
err := testhelpers.RunCobraCommand(cliCommand.Command, "--id", "12345", "--protocol-uuid", "abc123", "--front-protocol", "HTTP", "--back-protocol", "HTTP", "--front-port", "80", "--back-port", "80", "--method", "ROUNDROBIN", "--client-timeout", "100", "--server-timeout", "100", "--sticky", "cookie", "--connections", "5")
6568
Expect(err).NotTo(HaveOccurred())
@@ -77,6 +80,19 @@ var _ = Describe("LoadBal_protocol-edit_Test", func() {
7780
Expect(err).To(HaveOccurred())
7881
Expect(err.Error()).To(ContainSubstring("Value of option '--sticky' should be cookie or source-ip"))
7982
})
83+
It("--ssl-id option", func() {
84+
err := testhelpers.RunCobraCommand(cliCommand.Command, "--id", "12345", "--protocol-uuid=aaaa", "--ssl-id=9999", "--front-protocol=HTTPS")
85+
Expect(err).NotTo(HaveOccurred())
86+
lbUUID, argsForCall := fakeLBManager.AddLoadBalancerListenerArgsForCall(0)
87+
Expect(*lbUUID).To(Equal("aaa-bbb-111"))
88+
Expect(len(argsForCall)).To(Equal(1))
89+
// Making sure we are not sending in options we did not specify
90+
Expect(argsForCall[0].BackendProtocol).To(BeNil())
91+
92+
Expect(*argsForCall[0].FrontendProtocol).To(Equal("HTTPS"))
93+
Expect(*argsForCall[0].TlsCertificateId).To(Equal(9999))
94+
Expect(fakeUI.Outputs()).To(ContainSubstring("OK"))
95+
})
8096
})
8197

8298
Context("API Error", func() {

plugin/commands/loadbal/protocols_add.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type ProtocolAddCommand struct {
2626
Sticky string
2727
ClientTimeout int
2828
ServerTimeout int
29-
SslId int
29+
SslId int
3030
}
3131

3232
func NewProtocolAddCommand(sl *metadata.SoftlayerCommand) *ProtocolAddCommand {
@@ -37,15 +37,15 @@ func NewProtocolAddCommand(sl *metadata.SoftlayerCommand) *ProtocolAddCommand {
3737
cobraCmd := &cobra.Command{
3838
Use: "protocol-add",
3939
Short: T("Add a new load balancer protocol"),
40-
Long: T(`Creates a new mapping between incoming traffic to the loadbalancer and the backend servers.
40+
Long: T(`Creates a new mapping between incoming traffic to the loadbalancer and the backend servers.
4141
Use '{COMMAND_NAME} sl security cert-list' to get IDs for the --ssl-id option.
4242
See: https://cloud.ibm.com/docs/loadbalancer-service?topic=loadbalancer-service-about-ibm-cloud-load-balancer for more details
4343
4444
Example:
4545
${COMMAND_NAME} sl loadbal protocol-add --id 1115129 --front-port 443 --front-protocol HTTPS --back-port 80 --back-protocol HTTP --ssl-id 335659 --client-timeout 60 --connections 100
4646
Creates a new protocol on Load Balancer 1115129 that terminates SSL on port 443, mapping to a backend port 80 HTTP. Using SSL cert 335659
4747
`),
48-
Args: metadata.NoArgs,
48+
Args: metadata.NoArgs,
4949
RunE: func(cmd *cobra.Command, args []string) error {
5050
return thisCmd.Run(args)
5151
},
@@ -71,7 +71,6 @@ func (cmd *ProtocolAddCommand) Run(args []string) error {
7171
return errors.NewMissingInputError("--id")
7272
}
7373

74-
7574
loadbalancerUUID, err := cmd.LoadBalancerManager.GetLoadBalancerUUID(loadbalID)
7675
if err != nil {
7776
return errors.New(T("Failed to get load balancer: {{.ERR}}.", map[string]interface{}{"ERR": err.Error()}))

plugin/i18n/v2Resources/active.en-US.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,6 @@
188188
"${COMMAND_NAME} sl loadbal protocol-delete (--lb-id LOADBAL_ID) (--protocol-uuid PROTOCOL_UUID)": {
189189
"other": "${COMMAND_NAME} sl loadbal protocol-delete (--lb-id LOADBAL_ID) (--protocol-uuid PROTOCOL_UUID)"
190190
},
191-
"${COMMAND_NAME} sl loadbal protocol-edit (--id LOADBAL_ID) (--protocol-uuid PROTOCOL_UUID) [--front-protocol PROTOCOL] [back-protocol PROTOCOL] [--front-port PORT] [--back-port PORT] [-m, --method METHOD] [-c, --connections CONNECTIONS] [--sticky cookie | source-ip] [--client-timeout SECONDS] [--server-timeout SECONDS]": {
192-
"other": "${COMMAND_NAME} sl loadbal protocol-edit (--id LOADBAL_ID) (--protocol-uuid PROTOCOL_UUID) [--front-protocol PROTOCOL] [back-protocol PROTOCOL] [--front-port PORT] [--back-port PORT] [-m, --method METHOD] [-c, --connections CONNECTIONS] [--sticky cookie | source-ip] [--client-timeout SECONDS] [--server-timeout SECONDS]"
193-
},
194191
"${COMMAND_NAME} sl object-storage accounts": {
195192
"other": "${COMMAND_NAME} sl object-storage accounts"
196193
},
@@ -7097,6 +7094,9 @@
70977094
"Usage information.": {
70987095
"other": "Usage information."
70997096
},
7097+
"Use '${COMMAND_NAME} sl loadbal detail' to find the --protocol-uuid values for a loadbalancer\nExample:\n\t${COMMAND_NAME} sl loadbal protocol-add --id 1115129 --protocol-uuid 8ec8911a-c32d-4678-89fe-979f182c822f --ssl-id 123\n\tThis command changes the SSL certificate\n": {
7098+
"other": "Use '${COMMAND_NAME} sl loadbal detail' to find the --protocol-uuid values for a loadbalancer\nExample:\n\t${COMMAND_NAME} sl loadbal protocol-add --id 1115129 --protocol-uuid 8ec8911a-c32d-4678-89fe-979f182c822f --ssl-id 123\n\tThis command changes the SSL certificate\n"
7099+
},
71007100
"Use 'cookie' or 'source-ip' to stick": {
71017101
"other": "Use 'cookie' or 'source-ip' to stick"
71027102
},

0 commit comments

Comments
 (0)