Skip to content

Commit a0d2d91

Browse files
allmightyspiffGitHub Enterprise
authored andcommitted
Merge pull request #933 from SoftLayer/issues931
Fixed issues with securitygroup interface-add and interface-remove.
2 parents 16d91bb + e542c22 commit a0d2d91

File tree

4 files changed

+50
-90
lines changed

4 files changed

+50
-90
lines changed

plugin/commands/securitygroup/interface_add.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"strconv"
66
"strings"
77

8-
"github.com/softlayer/softlayer-go/datatypes"
98
bmxErr "github.ibm.com/SoftLayer/softlayer-cli/plugin/errors"
109
slErrors "github.ibm.com/SoftLayer/softlayer-cli/plugin/errors"
1110

@@ -29,6 +28,7 @@ func NewInterfaceAddCommand(sl *metadata.SoftlayerCommand) (cmd *InterfaceAddCom
2928
thisCmd := &InterfaceAddCommand{
3029
SoftlayerCommand: sl,
3130
NetworkManager: managers.NewNetworkManager(sl.Session),
31+
VSManager: managers.NewVirtualServerManager(sl.Session),
3232
}
3333

3434
cobraCmd := &cobra.Command{
@@ -90,26 +90,27 @@ func ValidateArgs(networkComponent int, serverId int, serverInterface string) er
9090

9191
func GetComponentId(vsManager managers.VirtualServerManager, networkComponent int, serverId int, serverInterface string) (int, error) {
9292
useServer := networkComponent == 0 && (serverId != 0 && serverInterface != "")
93+
9394
if useServer {
94-
vs, err := vsManager.GetInstance(serverId, "networkComponents[id,port]")
95+
vs, err := vsManager.GetInstance(serverId, "primaryBackendNetworkComponent[id,port], primaryNetworkComponent[id,port]")
9596
if err != nil {
9697
return 0, err
9798
}
98-
port := 0
99+
99100
if strings.ToLower(serverInterface) == "public" {
100-
port = 1
101-
}
102-
var component []datatypes.Virtual_Guest_Network_Component
103-
for _, c := range vs.NetworkComponents {
104-
if c.Port != nil && *c.Port == port {
105-
component = append(component, c)
101+
if vs.PrimaryNetworkComponent != nil && vs.PrimaryNetworkComponent.Id != nil {
102+
return *vs.PrimaryNetworkComponent.Id, nil
103+
}
104+
} else {
105+
if vs.PrimaryBackendNetworkComponent != nil && vs.PrimaryBackendNetworkComponent.Id != nil {
106+
return *vs.PrimaryBackendNetworkComponent.Id, nil
106107
}
107108
}
108-
if len(component) != 1 {
109-
return 0, errors.New(T("Instance {{.ServerID}} has {{.Count}} {{.Interface}} interface.",
110-
map[string]interface{}{"ServerID": serverId, "Interface": serverInterface, "Count": len(component)}))
111-
}
112-
return *component[0].Id, nil
109+
110+
return 0, errors.New(
111+
T("Instance {{.ServerID}} has {{.Count}} {{.Interface}} interface.",
112+
map[string]interface{}{"ServerID": serverId, "Interface": serverInterface, "Count": 0}))
113+
113114
}
114115
return networkComponent, nil
115116
}

plugin/commands/securitygroup/interface_add_test.go

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -112,50 +112,31 @@ var _ = Describe("Securitygroup interface add", func() {
112112
BeforeEach(func() {
113113
fakeVSManager.GetInstanceReturns(datatypes.Virtual_Guest{
114114
Id: sl.Int(4321),
115-
NetworkComponents: []datatypes.Virtual_Guest_Network_Component{
116-
datatypes.Virtual_Guest_Network_Component{
117-
Id: sl.Int(4567),
118-
Port: sl.Int(1),
119-
},
120-
datatypes.Virtual_Guest_Network_Component{
121-
Id: sl.Int(4569),
122-
Port: sl.Int(1),
123-
},
124-
datatypes.Virtual_Guest_Network_Component{
125-
Id: sl.Int(4568),
126-
Port: sl.Int(0),
127-
},
128-
datatypes.Virtual_Guest_Network_Component{
129-
Id: sl.Int(4566),
130-
Port: sl.Int(0),
131-
},
132-
},
115+
NetworkComponents: []datatypes.Virtual_Guest_Network_Component{},
133116
}, nil)
134117
})
135118
It("return error", func() {
136119
err := testhelpers.RunCobraCommand(cliCommand.Command, "1234", "-s", "4321", "-i", "public")
137120
Expect(err).To(HaveOccurred())
138-
Expect(err.Error()).To(ContainSubstring("Instance 4321 has 2 public interface."))
121+
Expect(err.Error()).To(ContainSubstring("Instance 4321 has 0 public interface."))
139122
})
140123
It("return error", func() {
141124
err := testhelpers.RunCobraCommand(cliCommand.Command, "1234", "-s", "4321", "-i", "private")
142125
Expect(err).To(HaveOccurred())
143-
Expect(err.Error()).To(ContainSubstring("Instance 4321 has 2 private interface."))
126+
Expect(err.Error()).To(ContainSubstring("Instance 4321 has 0 private interface."))
144127
})
145128
})
146129
Context("interface add with serverID but attach API call fails", func() {
147130
BeforeEach(func() {
148131
fakeVSManager.GetInstanceReturns(datatypes.Virtual_Guest{
149132
Id: sl.Int(4321),
150-
NetworkComponents: []datatypes.Virtual_Guest_Network_Component{
151-
datatypes.Virtual_Guest_Network_Component{
152-
Id: sl.Int(4567),
153-
Port: sl.Int(1),
154-
},
155-
datatypes.Virtual_Guest_Network_Component{
156-
Id: sl.Int(4568),
157-
Port: sl.Int(0),
158-
},
133+
PrimaryNetworkComponent: &datatypes.Virtual_Guest_Network_Component{
134+
Id: sl.Int(4567),
135+
Port: sl.Int(1),
136+
},
137+
PrimaryBackendNetworkComponent: &datatypes.Virtual_Guest_Network_Component{
138+
Id: sl.Int(4568),
139+
Port: sl.Int(0),
159140
},
160141
}, nil)
161142
fakeNetworkManager.AttachSecurityGroupComponentReturns(errors.New("Internal server error"))
@@ -177,15 +158,13 @@ var _ = Describe("Securitygroup interface add", func() {
177158
BeforeEach(func() {
178159
fakeVSManager.GetInstanceReturns(datatypes.Virtual_Guest{
179160
Id: sl.Int(4321),
180-
NetworkComponents: []datatypes.Virtual_Guest_Network_Component{
181-
datatypes.Virtual_Guest_Network_Component{
182-
Id: sl.Int(4567),
183-
Port: sl.Int(1),
184-
},
185-
datatypes.Virtual_Guest_Network_Component{
186-
Id: sl.Int(4568),
187-
Port: sl.Int(0),
188-
},
161+
PrimaryNetworkComponent: &datatypes.Virtual_Guest_Network_Component{
162+
Id: sl.Int(4567),
163+
Port: sl.Int(1),
164+
},
165+
PrimaryBackendNetworkComponent: &datatypes.Virtual_Guest_Network_Component{
166+
Id: sl.Int(4568),
167+
Port: sl.Int(0),
189168
},
190169
}, nil)
191170
fakeNetworkManager.AttachSecurityGroupComponentReturns(nil)

plugin/commands/securitygroup/interface_remove.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func NewInterfaceRemoveCommand(sl *metadata.SoftlayerCommand) (cmd *InterfaceRem
2525
thisCmd := &InterfaceRemoveCommand{
2626
SoftlayerCommand: sl,
2727
NetworkManager: managers.NewNetworkManager(sl.Session),
28+
VSManager: managers.NewVirtualServerManager(sl.Session),
2829
}
2930

3031
cobraCmd := &cobra.Command{

plugin/commands/securitygroup/interface_remove_test.go

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -112,50 +112,31 @@ var _ = Describe("Securitygroup interface remove", func() {
112112
BeforeEach(func() {
113113
fakeVSManager.GetInstanceReturns(datatypes.Virtual_Guest{
114114
Id: sl.Int(4321),
115-
NetworkComponents: []datatypes.Virtual_Guest_Network_Component{
116-
datatypes.Virtual_Guest_Network_Component{
117-
Id: sl.Int(4567),
118-
Port: sl.Int(1),
119-
},
120-
datatypes.Virtual_Guest_Network_Component{
121-
Id: sl.Int(4569),
122-
Port: sl.Int(1),
123-
},
124-
datatypes.Virtual_Guest_Network_Component{
125-
Id: sl.Int(4568),
126-
Port: sl.Int(0),
127-
},
128-
datatypes.Virtual_Guest_Network_Component{
129-
Id: sl.Int(4566),
130-
Port: sl.Int(0),
131-
},
132-
},
115+
NetworkComponents: []datatypes.Virtual_Guest_Network_Component{},
133116
}, nil)
134117
})
135118
It("return error", func() {
136119
err := testhelpers.RunCobraCommand(cliCommand.Command, "1234", "-s", "4321", "-i", "public")
137120
Expect(err).To(HaveOccurred())
138-
Expect(err.Error()).To(ContainSubstring("Instance 4321 has 2 public interface."))
121+
Expect(err.Error()).To(ContainSubstring("Instance 4321 has 0 public interface."))
139122
})
140123
It("return error", func() {
141124
err := testhelpers.RunCobraCommand(cliCommand.Command, "1234", "-s", "4321", "-i", "private")
142125
Expect(err).To(HaveOccurred())
143-
Expect(err.Error()).To(ContainSubstring("Instance 4321 has 2 private interface."))
126+
Expect(err.Error()).To(ContainSubstring("Instance 4321 has 0 private interface."))
144127
})
145128
})
146129
Context("interface remove with serverID but attach API call fails", func() {
147130
BeforeEach(func() {
148131
fakeVSManager.GetInstanceReturns(datatypes.Virtual_Guest{
149132
Id: sl.Int(4321),
150-
NetworkComponents: []datatypes.Virtual_Guest_Network_Component{
151-
datatypes.Virtual_Guest_Network_Component{
152-
Id: sl.Int(4567),
153-
Port: sl.Int(1),
154-
},
155-
datatypes.Virtual_Guest_Network_Component{
156-
Id: sl.Int(4568),
157-
Port: sl.Int(0),
158-
},
133+
PrimaryNetworkComponent: &datatypes.Virtual_Guest_Network_Component{
134+
Id: sl.Int(4567),
135+
Port: sl.Int(1),
136+
},
137+
PrimaryBackendNetworkComponent: &datatypes.Virtual_Guest_Network_Component{
138+
Id: sl.Int(4568),
139+
Port: sl.Int(0),
159140
},
160141
}, nil)
161142
fakeNetworkManager.DetachSecurityGroupComponentReturns(errors.New("Internal server error"))
@@ -177,15 +158,13 @@ var _ = Describe("Securitygroup interface remove", func() {
177158
BeforeEach(func() {
178159
fakeVSManager.GetInstanceReturns(datatypes.Virtual_Guest{
179160
Id: sl.Int(4321),
180-
NetworkComponents: []datatypes.Virtual_Guest_Network_Component{
181-
datatypes.Virtual_Guest_Network_Component{
182-
Id: sl.Int(4567),
183-
Port: sl.Int(1),
184-
},
185-
datatypes.Virtual_Guest_Network_Component{
186-
Id: sl.Int(4568),
187-
Port: sl.Int(0),
188-
},
161+
PrimaryNetworkComponent: &datatypes.Virtual_Guest_Network_Component{
162+
Id: sl.Int(4567),
163+
Port: sl.Int(1),
164+
},
165+
PrimaryBackendNetworkComponent: &datatypes.Virtual_Guest_Network_Component{
166+
Id: sl.Int(4568),
167+
Port: sl.Int(0),
189168
},
190169
}, nil)
191170
fakeNetworkManager.AttachSecurityGroupComponentReturns(nil)

0 commit comments

Comments
 (0)