-
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.
* Add dhcpserver command * Address review comments * Fix review comments - 2
- Loading branch information
Showing
8 changed files
with
366 additions
and
0 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,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") | ||
} |
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,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") | ||
} |
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,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") | ||
} |
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,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") | ||
} |
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,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 | ||
}, | ||
} |
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,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) | ||
} |
Oops, something went wrong.