-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from mkumatag/ports
Add code to create, get and delete network port
- Loading branch information
Showing
8 changed files
with
381 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
Oops, something went wrong.