Skip to content

Commit

Permalink
Add dhcpserver command (#326)
Browse files Browse the repository at this point in the history
* Add dhcpserver command

* Address review comments

* Fix review comments - 2
  • Loading branch information
mkumatag authored Aug 30, 2022
1 parent 8a78d94 commit a6fc272
Show file tree
Hide file tree
Showing 8 changed files with 366 additions and 0 deletions.
47 changes: 47 additions & 0 deletions cmd/dhcpserver/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2022 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 dhcpserver

import (
"fmt"

"github.com/spf13/cobra"

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

var Cmd = &cobra.Command{
Use: "dhcpserver",
Short: "dhcpserver command",
PreRunE: func(cmd *cobra.Command, args []string) error {
if pkg.Options.APIKey == "" {
return fmt.Errorf("api-key can't be empty, pass the token via --api-key or set IBMCLOUD_API_KEY environment variable")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
}

func init() {
Cmd.AddCommand(createCmd)
Cmd.AddCommand(listCmd)
Cmd.AddCommand(getCmd)
Cmd.AddCommand(deleteCmd)

Cmd.PersistentFlags().StringVarP(&pkg.Options.InstanceID, "instance-id", "i", "", "Instance ID of the PowerVS instance")
_ = Cmd.MarkPersistentFlagRequired("instance-id")
}
83 changes: 83 additions & 0 deletions cmd/dhcpserver/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2022 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 dhcpserver

import (
"fmt"
"github.com/IBM/go-sdk-core/v5/core"

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

"github.com/IBM-Cloud/power-go-client/power/models"
"github.com/ppc64le-cloud/pvsadm/pkg"
"github.com/ppc64le-cloud/pvsadm/pkg/client"
)

var (
cidr, dns, name, cloudConnectionID string
snat bool
)

var createCmd = &cobra.Command{
Use: "create",
Short: "Create DHCP Server",
Long: `Create DHCP Server`,
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
}

body := &models.DHCPServerCreate{
SnatEnabled: core.BoolPtr(snat),
}
if cidr != "" {
body.Cidr = core.StringPtr(cidr)
}
if cloudConnectionID != "" {
body.CloudConnectionID = core.StringPtr(cloudConnectionID)
}
if dns != "" {
body.DNSServer = core.StringPtr(dns)
}
if name != "" {
body.Name = core.StringPtr(name)
}
_, err = pvmclient.DHCPClient.Create(body)
if err != nil {
return fmt.Errorf("failed to create a dhcpserver, err: %v", err)
}

klog.Infof("Successfully created a DHCP server")
return nil
},
}

func init() {
createCmd.Flags().StringVar(&cidr, "cidr", "", "CIDR")
createCmd.Flags().StringVar(&cloudConnectionID, "cloud-connection-id", "", "Instance ID of the Cloud connection")
createCmd.Flags().StringVar(&dns, "dns-server", "", "DNS Server")
createCmd.Flags().StringVar(&name, "name", "", "Name")
createCmd.Flags().BoolVar(&snat, "snat", true, "SNAT Enabled")
}
59 changes: 59 additions & 0 deletions cmd/dhcpserver/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2022 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 dhcpserver

import (
"fmt"

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

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

var dhcp string
var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete DHCP Server",
Long: `Delete DHCP Server`,
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
}

err = pvmclient.DHCPClient.Delete(dhcp)
if err != nil {
return fmt.Errorf("failed to delete a dhcpserver, err: %v", err)
}

klog.Infof("Successfully Deleted a DHCP server")
return nil
},
}

func init() {
deleteCmd.Flags().StringVar(&dhcp, "id", "", "Instance ID of the DHCP server to be deleted")
_ = deleteCmd.MarkPersistentFlagRequired("id")
}
63 changes: 63 additions & 0 deletions cmd/dhcpserver/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2022 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 dhcpserver

import (
"fmt"

"github.com/davecgh/go-spew/spew"
"github.com/spf13/cobra"
"k8s.io/klog/v2"

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

var (
id string
)

var getCmd = &cobra.Command{
Use: "get",
Short: "Get DHCP Server",
Long: `Get DHCP Server`,
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
}

servers, err := pvmclient.DHCPClient.Get(id)
if err != nil {
return fmt.Errorf("failed to get a dhcpserver, err: %v", err)
}

spew.Dump(servers)
return nil
},
}

func init() {
getCmd.Flags().StringVar(&id, "id", "", "Instance ID of the Cloud connection")
_ = getCmd.MarkFlagRequired("id")
}
55 changes: 55 additions & 0 deletions cmd/dhcpserver/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2022 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 dhcpserver

import (
"fmt"

"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 listCmd = &cobra.Command{
Use: "list",
Short: "Get PowerVS DHCP servers",
Long: `Get PowerVS DHCP servers`,
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
}

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

table := utils.NewTable()
table.Render(dhcpservers, nil)
return nil
},
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/ppc64le-cloud/pvsadm/cmd/create"
deletecmd "github.com/ppc64le-cloud/pvsadm/cmd/delete"
"github.com/ppc64le-cloud/pvsadm/cmd/dhcp-sync"
"github.com/ppc64le-cloud/pvsadm/cmd/dhcpserver"
"github.com/ppc64le-cloud/pvsadm/cmd/get"
"github.com/ppc64le-cloud/pvsadm/cmd/image"
"github.com/ppc64le-cloud/pvsadm/cmd/purge"
Expand Down Expand Up @@ -69,6 +70,7 @@ func init() {
rootCmd.AddCommand(create.Cmd)
rootCmd.AddCommand(deletecmd.Cmd)
rootCmd.AddCommand(dhcp.Cmd)
rootCmd.AddCommand(dhcpserver.Cmd)
rootCmd.PersistentFlags().StringVarP(&pkg.Options.APIKey, "api-key", "k", "", "IBMCLOUD API Key(env name: IBMCLOUD_API_KEY)")
rootCmd.PersistentFlags().StringVar(&pkg.Options.Environment, "env", client.DefaultEnv, "IBM Cloud Environments, supported are: ["+strings.Join(client.ListEnvironments(), ", ")+"]")
rootCmd.PersistentFlags().BoolVar(&pkg.Options.Debug, "debug", false, "Enable PowerVS debug option(ATTENTION: dev only option, may print sensitive data from APIs)")
Expand Down
54 changes: 54 additions & 0 deletions pkg/client/dhcp/dhcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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 dhcp

import (
"context"

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

type Client struct {
session *ibmpisession.IBMPISession
client *instance.IBMPIDhcpClient
instanceID string
}

func NewClient(sess *ibmpisession.IBMPISession, powerinstanceid string) *Client {
c := &Client{
session: sess,
instanceID: powerinstanceid,
}
c.client = instance.NewIBMPIDhcpClient(context.Background(), sess, powerinstanceid)
return c
}

func (c *Client) Get(id string) (*models.DHCPServerDetail, error) {
return c.client.Get(id)
}

func (c *Client) GetAll() (models.DHCPServers, error) {
return c.client.GetAll()
}

func (c *Client) Create(body *models.DHCPServerCreate) (*models.DHCPServer, error) {
return c.client.Create(body)
}

func (c *Client) Delete(id string) error {
return c.client.Delete(id)
}
Loading

0 comments on commit a6fc272

Please sign in to comment.