Skip to content

Commit

Permalink
add powervs network create cmd
Browse files Browse the repository at this point in the history
Signed-off-by: Yussuf Shaikh <[email protected]>
  • Loading branch information
yussufsh committed Mar 14, 2023
1 parent 8bd402a commit 8961508
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 21 deletions.
103 changes: 86 additions & 17 deletions cmd/capibmadm/cmd/powervs/network/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,119 @@ limitations under the License.
package network

import (
"context"
"fmt"
"strings"

"github.com/spf13/cobra"

v "github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/power/models"

logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"

"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/iam"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/powervs"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/utils"
)

type networkCreateOptions struct {
name string
dnsServers []string
name string
private bool
public bool
cidr string
dnsServers []string
gateway string
jumbo bool
ipAddressRanges []string
}

// CreateCommand function to create PowerVS network.
func CreateCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "create NETWORK_NAME",
Use: "create",
Short: "Create PowerVS network",
Example: `
# Create PowerVS network with name capi-network
# Create PowerVS network
export IBMCLOUD_API_KEY=<api-key>
capibmadm powervs network create capi-network --service-instance-id <service-instance-id>`,
Public network: capibmadm powervs network create --public --service-instance-id <service-instance-id> --zone <zone>
Private network: capibmadm powervs network create --private --cidr <cidr> --service-instance-id <service-instance-id> --zone <zone>
Private network with ip address ranges: capibmadm powervs network create --private --cidr <cidr> --ip-ranges <start-ip>-<end-ip>,<start-ip>-<end-ip> --service-instance-id <service-instance-id> --zone <zone>
`,
}

var netCreateOption networkCreateOptions
cmd.Flags().StringSliceVar(&netCreateOption.dnsServers, "dns-servers", []string{"8.8.8.8", "9.9.9.9"}, "Comma separated list of DNS Servers to use for this network")
cmd.Flags().StringVar(&netCreateOption.name, "name", "", "The name of the network")
cmd.Flags().BoolVar(&netCreateOption.public, "public", true, "Public (pub-vlan) network type")
cmd.Flags().BoolVar(&netCreateOption.private, "private", false, "Private (vlan) network type")
cmd.Flags().StringVar(&netCreateOption.cidr, "cidr", "", "The network CIDR. Required for private network type")
cmd.Flags().StringVar(&netCreateOption.name, "gateway", "", "The gateway ip address")
cmd.Flags().StringSliceVar(&netCreateOption.dnsServers, "dns-servers", []string{"8.8.8.8", "9.9.9.9"}, "Comma separated list of DNS Servers to use")
cmd.Flags().StringSliceVar(&netCreateOption.ipAddressRanges, "ip-ranges", []string{}, "Comma separated IP Address Ranges")
cmd.Flags().BoolVar(&netCreateOption.jumbo, "jumbo", false, "Enable MTU Jumbo Network")

// both cannot be provided, default is public
cmd.MarkFlagsMutuallyExclusive("private", "public")
// cidr is required for private vlan
cmd.MarkFlagsRequiredTogether("private", "cidr")

cmd.RunE = func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("network name is not provided")
}
netCreateOption.name = args[0]
if err := createNetwork(netCreateOption); err != nil {
return err
}
return nil
return createNetwork(cmd.Context(), netCreateOption)
}
return cmd
}

func createNetwork(netCreateOption networkCreateOptions) error {
func createNetwork(ctx context.Context, netCreateOption networkCreateOptions) error {
log := logf.Log
log.Info("Creating Power VS network", "name", netCreateOption.name, "service-instance-id", options.GlobalOptions.ServiceInstanceID, "dns-servers", netCreateOption.dnsServers)
//TODO: add network creation logic here
log.Info("Creating Power VS network", "service-instance-id", options.GlobalOptions.ServiceInstanceID, "zone", options.GlobalOptions.PowerVSZone)

accountID, err := utils.GetAccountID(ctx, iam.GetIAMAuth())
if err != nil {
return err
}
sess, err := powervs.NewPISession(accountID, options.GlobalOptions.PowerVSZone, options.GlobalOptions.Debug)
if err != nil {
return err
}

networkClient := v.NewIBMPINetworkClient(ctx, sess, options.GlobalOptions.ServiceInstanceID)

// default is public network
ntype := "pub-vlan"
if netCreateOption.private {
ntype = "vlan"
}

body := &models.NetworkCreate{
Name: netCreateOption.name,
Type: &ntype,
Cidr: netCreateOption.cidr,
DNSServers: netCreateOption.dnsServers,
Gateway: netCreateOption.gateway,
Jumbo: netCreateOption.jumbo,
}

var ipAddressRanges []*models.IPAddressRange
for _, ipRange := range netCreateOption.ipAddressRanges {
if ipRange != "" {
ipAddresses := strings.Split(ipRange, "-")
if len(ipAddresses) != 2 {
return fmt.Errorf("failed to read ip range, provide a range of IP addresses \"startIP-endIP\"")
}
ipAddressRanges = append(ipAddressRanges, &models.IPAddressRange{
StartingIPAddress: &ipAddresses[0],
EndingIPAddress: &ipAddresses[1],
})
}
}
body.IPAddressRanges = ipAddressRanges

network, err := networkClient.Create(body)
if err != nil {
return fmt.Errorf("failed to create a network, err: %v", err)
}
log.Info("Successfully created a network", "networkID", *network.NetworkID)

return nil
}
7 changes: 5 additions & 2 deletions cmd/capibmadm/cmd/powervs/network/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ package network
import (
"context"

v "github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/spf13/cobra"

v "github.com/IBM-Cloud/power-go-client/clients/instance"

logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"

"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/iam"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/powervs"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/utils"
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
)

// DeleteCommand function to delete network's port.
Expand Down
24 changes: 22 additions & 2 deletions docs/book/src/topics/capibmadm/powervs/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,40 @@
### 1. capibmadm powervs network create

#### Usage:
Create Power VS network.
Create PowerVS network.

#### Environmental Variable:
IBMCLOUD_API_KEY: IBM Cloud api key.

#### Arguments:
--service-instance-id: Power VS service instance id.

--cidr: The network CIDR. Required for private network type.

--name: The name of the network.

--public: Public (pub-vlan) network type (default true)

--private: Private (vlan) network type (default false)

--gateway: The gateway ip address.

--dns-servers: Comma separated list of DNS Servers to use for this network, Defaults to 8.8.8.8, 9.9.9.9.

--ip-ranges: Comma separated IP Address Ranges.

--jumbo: Enable MTU Jumbo Network.


#### Example:
```shell
export IBMCLOUD_API_KEY=<api-key>
capibmadm powervs network create <network-name> --service-instance-id <service-instance-id>
# Public network:
capibmadm powervs network create --public --service-instance-id <service-instance-id> --zone <zone>
# Private network:
capibmadm powervs network create --private --cidr <cidr> --service-instance-id <service-instance-id> --zone <zone>
# Private network with ip address ranges:
capibmadm powervs network create --private --cidr <cidr> --ip-ranges <start-ip>-<end-ip>,<start-ip>-<end-ip> --service-instance-id <service-instance-id> --zone <zone>
```


Expand Down

0 comments on commit 8961508

Please sign in to comment.