@@ -17,50 +17,119 @@ limitations under the License.
17
17
package network
18
18
19
19
import (
20
+ "context"
20
21
"fmt"
22
+ "strings"
21
23
22
24
"github.com/spf13/cobra"
23
25
26
+ v "github.com/IBM-Cloud/power-go-client/clients/instance"
27
+ "github.com/IBM-Cloud/power-go-client/power/models"
28
+
24
29
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
25
30
31
+ "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/iam"
32
+ "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/powervs"
26
33
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
34
+ "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/utils"
27
35
)
28
36
29
37
type networkCreateOptions struct {
30
- name string
31
- dnsServers []string
38
+ name string
39
+ private bool
40
+ public bool
41
+ cidr string
42
+ dnsServers []string
43
+ gateway string
44
+ jumbo bool
45
+ ipAddressRanges []string
32
46
}
33
47
34
48
// CreateCommand function to create PowerVS network.
35
49
func CreateCommand () * cobra.Command {
36
50
cmd := & cobra.Command {
37
- Use : "create NETWORK_NAME " ,
51
+ Use : "create" ,
38
52
Short : "Create PowerVS network" ,
39
53
Example : `
40
- # Create PowerVS network with name capi-network
54
+ # Create PowerVS network
41
55
export IBMCLOUD_API_KEY=<api-key>
42
- capibmadm powervs network create capi-network --service-instance-id <service-instance-id>` ,
56
+ Public network: capibmadm powervs network create --public --service-instance-id <service-instance-id> --zone <zone>
57
+ Private network: capibmadm powervs network create --private --cidr <cidr> --service-instance-id <service-instance-id> --zone <zone>
58
+ 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>
59
+ ` ,
43
60
}
44
61
45
62
var netCreateOption networkCreateOptions
46
- 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" )
63
+ cmd .Flags ().StringVar (& netCreateOption .name , "name" , "" , "The name of the network" )
64
+ cmd .Flags ().BoolVar (& netCreateOption .public , "public" , true , "Public (pub-vlan) network type" )
65
+ cmd .Flags ().BoolVar (& netCreateOption .private , "private" , false , "Private (vlan) network type" )
66
+ cmd .Flags ().StringVar (& netCreateOption .cidr , "cidr" , "" , "The network CIDR. Required for private network type" )
67
+ cmd .Flags ().StringVar (& netCreateOption .name , "gateway" , "" , "The gateway ip address" )
68
+ cmd .Flags ().StringSliceVar (& netCreateOption .dnsServers , "dns-servers" , []string {"8.8.8.8" , "9.9.9.9" }, "Comma separated list of DNS Servers to use" )
69
+ cmd .Flags ().StringSliceVar (& netCreateOption .ipAddressRanges , "ip-ranges" , []string {}, "Comma separated IP Address Ranges" )
70
+ cmd .Flags ().BoolVar (& netCreateOption .jumbo , "jumbo" , false , "Enable MTU Jumbo Network" )
71
+
72
+ // both cannot be provided, default is public
73
+ cmd .MarkFlagsMutuallyExclusive ("private" , "public" )
74
+ // cidr is required for private vlan
75
+ cmd .MarkFlagsRequiredTogether ("private" , "cidr" )
47
76
48
77
cmd .RunE = func (cmd * cobra.Command , args []string ) error {
49
- if len (args ) < 1 {
50
- return fmt .Errorf ("network name is not provided" )
51
- }
52
- netCreateOption .name = args [0 ]
53
- if err := createNetwork (netCreateOption ); err != nil {
54
- return err
55
- }
56
- return nil
78
+ return createNetwork (cmd .Context (), netCreateOption )
57
79
}
58
80
return cmd
59
81
}
60
82
61
- func createNetwork (netCreateOption networkCreateOptions ) error {
83
+ func createNetwork (ctx context. Context , netCreateOption networkCreateOptions ) error {
62
84
log := logf .Log
63
- log .Info ("Creating Power VS network" , "name" , netCreateOption .name , "service-instance-id" , options .GlobalOptions .ServiceInstanceID , "dns-servers" , netCreateOption .dnsServers )
64
- //TODO: add network creation logic here
85
+ log .Info ("Creating PowerVS network" , "service-instance-id" , options .GlobalOptions .ServiceInstanceID , "zone" , options .GlobalOptions .PowerVSZone )
86
+
87
+ accountID , err := utils .GetAccountID (ctx , iam .GetIAMAuth ())
88
+ if err != nil {
89
+ return err
90
+ }
91
+ sess , err := powervs .NewPISession (accountID , options .GlobalOptions .PowerVSZone , options .GlobalOptions .Debug )
92
+ if err != nil {
93
+ return err
94
+ }
95
+
96
+ networkClient := v .NewIBMPINetworkClient (ctx , sess , options .GlobalOptions .ServiceInstanceID )
97
+
98
+ // default is public network
99
+ ntype := "pub-vlan"
100
+ if netCreateOption .private {
101
+ ntype = "vlan"
102
+ }
103
+
104
+ body := & models.NetworkCreate {
105
+ Name : netCreateOption .name ,
106
+ Type : & ntype ,
107
+ Cidr : netCreateOption .cidr ,
108
+ DNSServers : netCreateOption .dnsServers ,
109
+ Gateway : netCreateOption .gateway ,
110
+ Jumbo : netCreateOption .jumbo ,
111
+ }
112
+
113
+ var ipAddressRanges []* models.IPAddressRange
114
+ for _ , ipRange := range netCreateOption .ipAddressRanges {
115
+ if ipRange != "" {
116
+ ipAddresses := strings .Split (ipRange , "-" )
117
+ if len (ipAddresses ) != 2 {
118
+ return fmt .Errorf ("failed to read ip range, provide a range of IP addresses \" startIP-endIP\" " )
119
+ }
120
+ ipAddressRanges = append (ipAddressRanges , & models.IPAddressRange {
121
+ StartingIPAddress : & ipAddresses [0 ],
122
+ EndingIPAddress : & ipAddresses [1 ],
123
+ })
124
+ }
125
+ }
126
+ body .IPAddressRanges = ipAddressRanges
127
+
128
+ network , err := networkClient .Create (body )
129
+ if err != nil {
130
+ return fmt .Errorf ("failed to create a network, err: %v" , err )
131
+ }
132
+ log .Info ("Successfully created a network" , "networkID" , * network .NetworkID )
133
+
65
134
return nil
66
135
}
0 commit comments