Skip to content

Commit

Permalink
Merge pull request #131 from mkumatag/ports
Browse files Browse the repository at this point in the history
Add code to create, get and delete network port
  • Loading branch information
ltccci authored Mar 23, 2021
2 parents b638fcf + a686b8a commit d575e0b
Show file tree
Hide file tree
Showing 8 changed files with 381 additions and 2 deletions.
34 changes: 34 additions & 0 deletions cmd/create/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2021 IBM Corp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package create

import (
"github.com/spf13/cobra"

"github.com/ppc64le-cloud/pvsadm/cmd/create/port"
"github.com/ppc64le-cloud/pvsadm/pkg"
)

var Cmd = &cobra.Command{
Use: "create",
Short: "Create the resources",
Long: `Create the resources`,
}

func init() {
Cmd.AddCommand(port.Cmd)
Cmd.PersistentFlags().StringVarP(&pkg.Options.InstanceID, "instance-id", "i", "", "Instance ID of the PowerVS instance")
_ = Cmd.MarkPersistentFlagRequired("instance-id")
}
102 changes: 102 additions & 0 deletions cmd/create/port/port.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2021 IBM Corp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package port

import (
"fmt"
"strings"

"github.com/IBM-Cloud/power-go-client/power/client/p_cloud_networks"
"github.com/IBM-Cloud/power-go-client/power/models"
"github.com/spf13/cobra"
"k8s.io/klog/v2"

"github.com/ppc64le-cloud/pvsadm/pkg"
"github.com/ppc64le-cloud/pvsadm/pkg/client"
"github.com/ppc64le-cloud/pvsadm/pkg/utils"
)

var (
network, ipaddress, description string
)

var Cmd = &cobra.Command{
Use: "port",
Short: "Create PowerVS network port",
Long: `Create PowerVS network port`,
RunE: func(cmd *cobra.Command, args []string) error {
opt := pkg.Options

c, err := client.NewClientWithEnv(opt.APIKey, opt.Environment, opt.Debug)
if err != nil {
klog.Errorf("failed to create a session with IBM cloud: %v", err)
return err
}

pvmclient, err := client.NewPVMClientWithEnv(c, opt.InstanceID, opt.InstanceName, opt.Environment)
if err != nil {
return err
}

networks, err := pvmclient.NetworkClient.GetAll()
if err != nil {
return fmt.Errorf("failed to get the networks, err: %v", err)
}

var networkNames, networkIDs []string
for _, net := range networks.Networks {
networkIDs = append(networkIDs, *net.NetworkID)
networkNames = append(networkNames, *net.Name)
}

var netID string

if utils.Contains(networkIDs, network) {
netID = network
} else if utils.Contains(networkNames, network) {
for _, n := range networks.Networks {
if *n.Name == network {
netID = *n.NetworkID
}
}
} else {
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, ","))
}

params := &p_cloud_networks.PcloudNetworksPortsPostParams{
Body: &models.NetworkPortCreate{
Description: description,
IPAddress: ipaddress,
},
}

port, err := pvmclient.NetworkClient.CreatePort(netID, params)
if err != nil {
return fmt.Errorf("failed to create a port, err: %v", err)
}
klog.Infof("Successfully created a port, id: %s", *port.PortID)

table := utils.NewTable()
table.Render([]*models.NetworkPort{port}, []string{})
return nil
},
}

func init() {
Cmd.Flags().StringVar(&network, "network", "", "Network ID or Name(preference will be given to the ID over Name)")
Cmd.Flags().StringVar(&ipaddress, "ip-address", "", "The requested ip address of this port")
Cmd.Flags().StringVar(&description, "description", "", "Description of the port")
_ = Cmd.MarkFlagRequired("network")
}
34 changes: 34 additions & 0 deletions cmd/delete/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2021 IBM Corp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package delete

import (
"github.com/spf13/cobra"

"github.com/ppc64le-cloud/pvsadm/cmd/delete/port"
"github.com/ppc64le-cloud/pvsadm/pkg"
)

var Cmd = &cobra.Command{
Use: "delete",
Short: "Delete the resources",
Long: `Delete the resources`,
}

func init() {
Cmd.AddCommand(port.Cmd)
Cmd.PersistentFlags().StringVarP(&pkg.Options.InstanceID, "instance-id", "i", "", "Instance ID of the PowerVS instance")
_ = Cmd.MarkPersistentFlagRequired("instance-id")
}
90 changes: 90 additions & 0 deletions cmd/delete/port/port.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2021 IBM Corp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package port

import (
"fmt"
"strings"

"github.com/spf13/cobra"
"k8s.io/klog/v2"

"github.com/ppc64le-cloud/pvsadm/pkg"
"github.com/ppc64le-cloud/pvsadm/pkg/client"
"github.com/ppc64le-cloud/pvsadm/pkg/utils"
)

var (
network, portID string
)

var Cmd = &cobra.Command{
Use: "port",
Short: "Delete PowerVS network port",
Long: `Delete PowerVS network port`,
RunE: func(cmd *cobra.Command, args []string) error {
opt := pkg.Options

c, err := client.NewClientWithEnv(opt.APIKey, opt.Environment, opt.Debug)
if err != nil {
klog.Errorf("failed to create a session with IBM cloud: %v", err)
return err
}

pvmclient, err := client.NewPVMClientWithEnv(c, opt.InstanceID, opt.InstanceName, opt.Environment)
if err != nil {
return err
}

networks, err := pvmclient.NetworkClient.GetAll()
if err != nil {
return fmt.Errorf("failed to get the networks, err: %v", err)
}

var networkNames, networkIDs []string
for _, net := range networks.Networks {
networkIDs = append(networkIDs, *net.NetworkID)
networkNames = append(networkNames, *net.Name)
}

var netID string

if utils.Contains(networkIDs, network) {
netID = network
} else if utils.Contains(networkNames, network) {
for _, n := range networks.Networks {
if *n.Name == network {
netID = *n.NetworkID
}
}
} else {
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, ","))
}

_, err = pvmclient.NetworkClient.DeletePort(netID, portID)
if err != nil {
return fmt.Errorf("failed to delete a port, err: %v", err)
}
klog.Infof("Successfully deleted a port, id: %s", portID)
return nil
},
}

func init() {
Cmd.Flags().StringVar(&network, "network", "", "Network ID or Name(preference will be given to the ID over Name)")
Cmd.Flags().StringVar(&portID, "port-id", "", "Port ID to be deleted")
_ = Cmd.MarkFlagRequired("network")
_ = Cmd.MarkFlagRequired("port-id")
}
5 changes: 4 additions & 1 deletion cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
package get

import (
"github.com/spf13/cobra"

"github.com/ppc64le-cloud/pvsadm/cmd/get/events"
"github.com/ppc64le-cloud/pvsadm/cmd/get/ports"
"github.com/ppc64le-cloud/pvsadm/pkg"
"github.com/spf13/cobra"
)

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

func init() {
Cmd.AddCommand(events.Cmd)
Cmd.AddCommand(ports.Cmd)
Cmd.PersistentFlags().StringVarP(&pkg.Options.InstanceID, "instance-id", "i", "", "Instance ID of the PowerVS instance")
Cmd.PersistentFlags().StringVarP(&pkg.Options.InstanceName, "instance-name", "n", "", "Instance name of the PowerVS")
}
96 changes: 96 additions & 0 deletions cmd/get/ports/ports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2021 IBM Corp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ports

import (
"fmt"
"strings"

"github.com/spf13/cobra"
"k8s.io/klog/v2"

"github.com/ppc64le-cloud/pvsadm/pkg"
"github.com/ppc64le-cloud/pvsadm/pkg/client"
"github.com/ppc64le-cloud/pvsadm/pkg/utils"
)

var (
network string
)

var Cmd = &cobra.Command{
Use: "ports",
Short: "Get PowerVS network ports",
Long: `Get PowerVS network ports`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if pkg.Options.InstanceID == "" && pkg.Options.InstanceName == "" {
return fmt.Errorf("--instance-id or --instance-name required")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
opt := pkg.Options

c, err := client.NewClientWithEnv(opt.APIKey, opt.Environment, opt.Debug)
if err != nil {
klog.Errorf("failed to create a session with IBM cloud: %v", err)
return err
}

pvmclient, err := client.NewPVMClientWithEnv(c, opt.InstanceID, opt.InstanceName, opt.Environment)
if err != nil {
return err
}

networks, err := pvmclient.NetworkClient.GetAll()
if err != nil {
return fmt.Errorf("failed to get the networks, err: %v", err)
}

var networkNames, networkIDs []string
for _, net := range networks.Networks {
networkIDs = append(networkIDs, *net.NetworkID)
networkNames = append(networkNames, *net.Name)
}

var netID string

if utils.Contains(networkIDs, network) {
netID = network
} else if utils.Contains(networkNames, network) {
for _, n := range networks.Networks {
if *n.Name == network {
netID = *n.NetworkID
}
}
} else {
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, ","))
}

ports, err := pvmclient.NetworkClient.GetAllPort(netID)
if err != nil {
return fmt.Errorf("failed to get the ports, err: %v", err)
}

table := utils.NewTable()
table.Render(ports.Ports, []string{"href", "pvminstance"})
return nil
},
}

func init() {
Cmd.Flags().StringVar(&network, "network", "", "Network ID or Name(preference will be given to the ID over Name)")
_ = Cmd.MarkFlagRequired("network")
}
Loading

0 comments on commit d575e0b

Please sign in to comment.