Skip to content

Commit d575e0b

Browse files
authored
Merge pull request #131 from mkumatag/ports
Add code to create, get and delete network port
2 parents b638fcf + a686b8a commit d575e0b

File tree

8 files changed

+381
-2
lines changed

8 files changed

+381
-2
lines changed

cmd/create/create.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2021 IBM Corp
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package create
16+
17+
import (
18+
"github.com/spf13/cobra"
19+
20+
"github.com/ppc64le-cloud/pvsadm/cmd/create/port"
21+
"github.com/ppc64le-cloud/pvsadm/pkg"
22+
)
23+
24+
var Cmd = &cobra.Command{
25+
Use: "create",
26+
Short: "Create the resources",
27+
Long: `Create the resources`,
28+
}
29+
30+
func init() {
31+
Cmd.AddCommand(port.Cmd)
32+
Cmd.PersistentFlags().StringVarP(&pkg.Options.InstanceID, "instance-id", "i", "", "Instance ID of the PowerVS instance")
33+
_ = Cmd.MarkPersistentFlagRequired("instance-id")
34+
}

cmd/create/port/port.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright 2021 IBM Corp
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package port
16+
17+
import (
18+
"fmt"
19+
"strings"
20+
21+
"github.com/IBM-Cloud/power-go-client/power/client/p_cloud_networks"
22+
"github.com/IBM-Cloud/power-go-client/power/models"
23+
"github.com/spf13/cobra"
24+
"k8s.io/klog/v2"
25+
26+
"github.com/ppc64le-cloud/pvsadm/pkg"
27+
"github.com/ppc64le-cloud/pvsadm/pkg/client"
28+
"github.com/ppc64le-cloud/pvsadm/pkg/utils"
29+
)
30+
31+
var (
32+
network, ipaddress, description string
33+
)
34+
35+
var Cmd = &cobra.Command{
36+
Use: "port",
37+
Short: "Create PowerVS network port",
38+
Long: `Create PowerVS network port`,
39+
RunE: func(cmd *cobra.Command, args []string) error {
40+
opt := pkg.Options
41+
42+
c, err := client.NewClientWithEnv(opt.APIKey, opt.Environment, opt.Debug)
43+
if err != nil {
44+
klog.Errorf("failed to create a session with IBM cloud: %v", err)
45+
return err
46+
}
47+
48+
pvmclient, err := client.NewPVMClientWithEnv(c, opt.InstanceID, opt.InstanceName, opt.Environment)
49+
if err != nil {
50+
return err
51+
}
52+
53+
networks, err := pvmclient.NetworkClient.GetAll()
54+
if err != nil {
55+
return fmt.Errorf("failed to get the networks, err: %v", err)
56+
}
57+
58+
var networkNames, networkIDs []string
59+
for _, net := range networks.Networks {
60+
networkIDs = append(networkIDs, *net.NetworkID)
61+
networkNames = append(networkNames, *net.Name)
62+
}
63+
64+
var netID string
65+
66+
if utils.Contains(networkIDs, network) {
67+
netID = network
68+
} else if utils.Contains(networkNames, network) {
69+
for _, n := range networks.Networks {
70+
if *n.Name == network {
71+
netID = *n.NetworkID
72+
}
73+
}
74+
} else {
75+
return fmt.Errorf("not able to find network: \"%s\" by ID or name in the list: ids:[%s], names: [%s]", network, strings.Join(networkIDs, ","), strings.Join(networkNames, ","))
76+
}
77+
78+
params := &p_cloud_networks.PcloudNetworksPortsPostParams{
79+
Body: &models.NetworkPortCreate{
80+
Description: description,
81+
IPAddress: ipaddress,
82+
},
83+
}
84+
85+
port, err := pvmclient.NetworkClient.CreatePort(netID, params)
86+
if err != nil {
87+
return fmt.Errorf("failed to create a port, err: %v", err)
88+
}
89+
klog.Infof("Successfully created a port, id: %s", *port.PortID)
90+
91+
table := utils.NewTable()
92+
table.Render([]*models.NetworkPort{port}, []string{})
93+
return nil
94+
},
95+
}
96+
97+
func init() {
98+
Cmd.Flags().StringVar(&network, "network", "", "Network ID or Name(preference will be given to the ID over Name)")
99+
Cmd.Flags().StringVar(&ipaddress, "ip-address", "", "The requested ip address of this port")
100+
Cmd.Flags().StringVar(&description, "description", "", "Description of the port")
101+
_ = Cmd.MarkFlagRequired("network")
102+
}

cmd/delete/delete.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2021 IBM Corp
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package delete
16+
17+
import (
18+
"github.com/spf13/cobra"
19+
20+
"github.com/ppc64le-cloud/pvsadm/cmd/delete/port"
21+
"github.com/ppc64le-cloud/pvsadm/pkg"
22+
)
23+
24+
var Cmd = &cobra.Command{
25+
Use: "delete",
26+
Short: "Delete the resources",
27+
Long: `Delete the resources`,
28+
}
29+
30+
func init() {
31+
Cmd.AddCommand(port.Cmd)
32+
Cmd.PersistentFlags().StringVarP(&pkg.Options.InstanceID, "instance-id", "i", "", "Instance ID of the PowerVS instance")
33+
_ = Cmd.MarkPersistentFlagRequired("instance-id")
34+
}

cmd/delete/port/port.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright 2021 IBM Corp
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package port
16+
17+
import (
18+
"fmt"
19+
"strings"
20+
21+
"github.com/spf13/cobra"
22+
"k8s.io/klog/v2"
23+
24+
"github.com/ppc64le-cloud/pvsadm/pkg"
25+
"github.com/ppc64le-cloud/pvsadm/pkg/client"
26+
"github.com/ppc64le-cloud/pvsadm/pkg/utils"
27+
)
28+
29+
var (
30+
network, portID string
31+
)
32+
33+
var Cmd = &cobra.Command{
34+
Use: "port",
35+
Short: "Delete PowerVS network port",
36+
Long: `Delete PowerVS network port`,
37+
RunE: func(cmd *cobra.Command, args []string) error {
38+
opt := pkg.Options
39+
40+
c, err := client.NewClientWithEnv(opt.APIKey, opt.Environment, opt.Debug)
41+
if err != nil {
42+
klog.Errorf("failed to create a session with IBM cloud: %v", err)
43+
return err
44+
}
45+
46+
pvmclient, err := client.NewPVMClientWithEnv(c, opt.InstanceID, opt.InstanceName, opt.Environment)
47+
if err != nil {
48+
return err
49+
}
50+
51+
networks, err := pvmclient.NetworkClient.GetAll()
52+
if err != nil {
53+
return fmt.Errorf("failed to get the networks, err: %v", err)
54+
}
55+
56+
var networkNames, networkIDs []string
57+
for _, net := range networks.Networks {
58+
networkIDs = append(networkIDs, *net.NetworkID)
59+
networkNames = append(networkNames, *net.Name)
60+
}
61+
62+
var netID string
63+
64+
if utils.Contains(networkIDs, network) {
65+
netID = network
66+
} else if utils.Contains(networkNames, network) {
67+
for _, n := range networks.Networks {
68+
if *n.Name == network {
69+
netID = *n.NetworkID
70+
}
71+
}
72+
} else {
73+
return fmt.Errorf("not able to find network: \"%s\" by ID or name in the list: ids:[%s], names: [%s]", network, strings.Join(networkIDs, ","), strings.Join(networkNames, ","))
74+
}
75+
76+
_, err = pvmclient.NetworkClient.DeletePort(netID, portID)
77+
if err != nil {
78+
return fmt.Errorf("failed to delete a port, err: %v", err)
79+
}
80+
klog.Infof("Successfully deleted a port, id: %s", portID)
81+
return nil
82+
},
83+
}
84+
85+
func init() {
86+
Cmd.Flags().StringVar(&network, "network", "", "Network ID or Name(preference will be given to the ID over Name)")
87+
Cmd.Flags().StringVar(&portID, "port-id", "", "Port ID to be deleted")
88+
_ = Cmd.MarkFlagRequired("network")
89+
_ = Cmd.MarkFlagRequired("port-id")
90+
}

cmd/get/get.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
package get
1616

1717
import (
18+
"github.com/spf13/cobra"
19+
1820
"github.com/ppc64le-cloud/pvsadm/cmd/get/events"
21+
"github.com/ppc64le-cloud/pvsadm/cmd/get/ports"
1922
"github.com/ppc64le-cloud/pvsadm/pkg"
20-
"github.com/spf13/cobra"
2123
)
2224

2325
var Cmd = &cobra.Command{
@@ -28,6 +30,7 @@ var Cmd = &cobra.Command{
2830

2931
func init() {
3032
Cmd.AddCommand(events.Cmd)
33+
Cmd.AddCommand(ports.Cmd)
3134
Cmd.PersistentFlags().StringVarP(&pkg.Options.InstanceID, "instance-id", "i", "", "Instance ID of the PowerVS instance")
3235
Cmd.PersistentFlags().StringVarP(&pkg.Options.InstanceName, "instance-name", "n", "", "Instance name of the PowerVS")
3336
}

cmd/get/ports/ports.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright 2021 IBM Corp
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package ports
16+
17+
import (
18+
"fmt"
19+
"strings"
20+
21+
"github.com/spf13/cobra"
22+
"k8s.io/klog/v2"
23+
24+
"github.com/ppc64le-cloud/pvsadm/pkg"
25+
"github.com/ppc64le-cloud/pvsadm/pkg/client"
26+
"github.com/ppc64le-cloud/pvsadm/pkg/utils"
27+
)
28+
29+
var (
30+
network string
31+
)
32+
33+
var Cmd = &cobra.Command{
34+
Use: "ports",
35+
Short: "Get PowerVS network ports",
36+
Long: `Get PowerVS network ports`,
37+
PreRunE: func(cmd *cobra.Command, args []string) error {
38+
if pkg.Options.InstanceID == "" && pkg.Options.InstanceName == "" {
39+
return fmt.Errorf("--instance-id or --instance-name required")
40+
}
41+
return nil
42+
},
43+
RunE: func(cmd *cobra.Command, args []string) error {
44+
opt := pkg.Options
45+
46+
c, err := client.NewClientWithEnv(opt.APIKey, opt.Environment, opt.Debug)
47+
if err != nil {
48+
klog.Errorf("failed to create a session with IBM cloud: %v", err)
49+
return err
50+
}
51+
52+
pvmclient, err := client.NewPVMClientWithEnv(c, opt.InstanceID, opt.InstanceName, opt.Environment)
53+
if err != nil {
54+
return err
55+
}
56+
57+
networks, err := pvmclient.NetworkClient.GetAll()
58+
if err != nil {
59+
return fmt.Errorf("failed to get the networks, err: %v", err)
60+
}
61+
62+
var networkNames, networkIDs []string
63+
for _, net := range networks.Networks {
64+
networkIDs = append(networkIDs, *net.NetworkID)
65+
networkNames = append(networkNames, *net.Name)
66+
}
67+
68+
var netID string
69+
70+
if utils.Contains(networkIDs, network) {
71+
netID = network
72+
} else if utils.Contains(networkNames, network) {
73+
for _, n := range networks.Networks {
74+
if *n.Name == network {
75+
netID = *n.NetworkID
76+
}
77+
}
78+
} else {
79+
return fmt.Errorf("not able to find network: \"%s\" by ID or name in the list: ids:[%s], names: [%s]", network, strings.Join(networkIDs, ","), strings.Join(networkNames, ","))
80+
}
81+
82+
ports, err := pvmclient.NetworkClient.GetAllPort(netID)
83+
if err != nil {
84+
return fmt.Errorf("failed to get the ports, err: %v", err)
85+
}
86+
87+
table := utils.NewTable()
88+
table.Render(ports.Ports, []string{"href", "pvminstance"})
89+
return nil
90+
},
91+
}
92+
93+
func init() {
94+
Cmd.Flags().StringVar(&network, "network", "", "Network ID or Name(preference will be given to the ID over Name)")
95+
_ = Cmd.MarkFlagRequired("network")
96+
}

0 commit comments

Comments
 (0)