Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor dhcp and dhcps data source and documentation #5056

Merged
merged 17 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 28 additions & 38 deletions ibm/service/power/data_source_ibm_pi_dhcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,113 +6,103 @@ package power
import (
"context"
"fmt"

"log"

st "github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func DataSourceIBMPIDhcp() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMPIDhcpRead,
Schema: map[string]*schema.Schema{

// Required Arguments
// Arguments
Arg_CloudInstanceID: {
Type: schema.TypeString,
Description: "The GUID of the service instance associated with an account.",
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
Arg_DhcpID: {
Type: schema.TypeString,
Description: "ID of the DHCP Server.",
Required: true,
Description: "The ID of the DHCP Server",
Type: schema.TypeString,
},

// Attributes
Attr_DhcpID: {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the DHCP Server",
Deprecated: "The field is deprecated, use pi_dhcp_id instead.",
Description: "ID of the DHCP Server.",
Type: schema.TypeString,
},
Attr_DhcpLeases: {
Type: schema.TypeList,
Attr_Leases: {
Computed: true,
Description: "The list of DHCP Server PVM Instance leases",
Description: "List of DHCP Server PVM Instance leases.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Attr_DhcpLeaseInstanceIP: {
Type: schema.TypeString,
Computed: true,
Description: "The IP of the PVM Instance",
Description: "IP of the PVM Instance.",
Type: schema.TypeString,
},
Attr_DhcpLeaseInstanceMac: {
Type: schema.TypeString,
Computed: true,
Description: "The MAC Address of the PVM Instance",
Description: "MAC Address of the PVM Instance.",
Type: schema.TypeString,
},
},
},
Type: schema.TypeList,
},
Attr_DhcpNetworkID: {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the DHCP Server private network",
},
Attr_DhcpNetworkName: {
Description: "ID of the DHCP Server private network.",
Type: schema.TypeString,
Computed: true,
Description: "The name of the DHCP Server private network",
},
Attr_DhcpStatus: {
Attr_NetworkName: {
Computed: true,
Description: "Name of the DHCP Server private network.",
Type: schema.TypeString,
},
Attr_Status: {
Computed: true,
Description: "The status of the DHCP Server",
Description: "Status of the DHCP Server.",
Type: schema.TypeString,
},
},
}
}

func dataSourceIBMPIDhcpRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {

// session
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}

// arguments
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
dhcpID := d.Get(Arg_DhcpID).(string)

// client
client := st.NewIBMPIDhcpClient(ctx, sess, cloudInstanceID)

// get dhcp
client := instance.NewIBMPIDhcpClient(ctx, sess, cloudInstanceID)
dhcpServer, err := client.Get(dhcpID)
if err != nil {
log.Printf("[DEBUG] get DHCP failed %v", err)
return diag.FromErr(err)
}

// set attributes
d.SetId(fmt.Sprintf("%s/%s", cloudInstanceID, *dhcpServer.ID))
d.Set(Attr_DhcpID, *dhcpServer.ID)
d.Set(Attr_DhcpStatus, *dhcpServer.Status)
d.Set(Attr_Status, *dhcpServer.Status)

if dhcpServer.Network != nil {
dhcpNetwork := dhcpServer.Network
if dhcpNetwork.ID != nil {
d.Set(Attr_DhcpNetworkID, *dhcpNetwork.ID)
}
if dhcpNetwork.Name != nil {
d.Set(Attr_DhcpNetworkName, *dhcpNetwork.Name)
d.Set(Attr_NetworkName, *dhcpNetwork.Name)
}
}

Expand All @@ -124,7 +114,7 @@ func dataSourceIBMPIDhcpRead(ctx context.Context, d *schema.ResourceData, meta i
Attr_DhcpLeaseInstanceMac: *lease.InstanceMacAddress,
}
}
d.Set(Attr_DhcpLeases, leaseList)
d.Set(Attr_Leases, leaseList)
}

return nil
Expand Down
1 change: 0 additions & 1 deletion ibm/service/power/data_source_ibm_pi_dhcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
)

func TestAccIBMPIDhcpDataSourceBasic(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Expand Down
55 changes: 21 additions & 34 deletions ibm/service/power/data_source_ibm_pi_dhcps.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,101 +7,88 @@ import (
"context"
"log"

st "github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

/*
Datasource to get the list of dhcp servers in a power instance
*/

// Datasource to list dhcp servers in a power instance
func DataSourceIBMPIDhcps() *schema.Resource {

return &schema.Resource{
ReadContext: dataSourceIBMPIDhcpServersRead,
Schema: map[string]*schema.Schema{

// Required Arguments
// Arguments
Arg_CloudInstanceID: {
Type: schema.TypeString,
Description: "The GUID of the service instance associated with an account.",
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},

// Attributes
Attr_DhcpServers: {
Type: schema.TypeList,
Computed: true,
Description: "The list of all the DHCP Servers",
Description: "List of all the DHCP Servers.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Attr_DhcpID: {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the DHCP Server",
Description: "ID of the DHCP Server.",
Type: schema.TypeString,
},
Attr_DhcpNetworkID: {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the DHCP Server private network",
},
Attr_DhcpNetworkName: {
Description: "ID of the DHCP Server private network.",
Type: schema.TypeString,
Computed: true,
Description: "The name of the DHCP Server private network",
},
Attr_DhcpStatus: {
Attr_NetworkName: {
Computed: true,
Description: "Name of the DHCP Server private network.",
Type: schema.TypeString,
},
Attr_Status: {
Computed: true,
Description: "The status of the DHCP Server",
Description: "Status of the DHCP Server.",
Type: schema.TypeString,
},
},
},
Type: schema.TypeList,
},
},
}
}

func dataSourceIBMPIDhcpServersRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {

// session and client
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}

// arguments
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)

// client
client := st.NewIBMPIDhcpClient(ctx, sess, cloudInstanceID)

// get all dhcp
client := instance.NewIBMPIDhcpClient(ctx, sess, cloudInstanceID)
dhcpServers, err := client.GetAll()
if err != nil {
log.Printf("[DEBUG] get all DHCP failed %v", err)
return diag.FromErr(err)
}

// set attributes
servers := make([]map[string]interface{}, 0, len(dhcpServers))
for _, dhcpServer := range dhcpServers {
server := map[string]interface{}{
Attr_DhcpID: *dhcpServer.ID,
Attr_DhcpStatus: *dhcpServer.Status,
Attr_DhcpID: *dhcpServer.ID,
Attr_Status: *dhcpServer.Status,
}
if dhcpServer.Network != nil {
dhcpNetwork := dhcpServer.Network
if dhcpNetwork.ID != nil {
d.Set(Attr_DhcpNetworkID, *dhcpNetwork.ID)
}
if dhcpNetwork.Name != nil {
d.Set(Attr_DhcpNetworkName, *dhcpNetwork.Name)
d.Set(Attr_NetworkName, *dhcpNetwork.Name)
}
}
servers = append(servers, server)
Expand Down
1 change: 0 additions & 1 deletion ibm/service/power/data_source_ibm_pi_dhcps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
)

func TestAccIBMPIDhcpServersDataSourceBasic(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Expand Down
7 changes: 3 additions & 4 deletions ibm/service/power/ibm_pi_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ const (
Attr_IsActive = "is_active"
Attr_Jumbo = "jumbo"
Attr_Key = "key"
Attr_KeyCreationDate = "creation_date"
Attr_KeyID = "key_id"
Attr_KeyName = "name"
Attr_Keys = "keys"
Expand Down Expand Up @@ -290,8 +291,6 @@ const (
Attr_IBMiRDSUsers = "ibmi_rds_users"
OS_IBMI = "ibmi"

Arg_PIInstanceSharedProcessorPool = "pi_shared_processor_pool"

PVMInstanceHealthOk = "OK"
PVMInstanceHealthWarning = "WARNING"

Expand Down Expand Up @@ -326,6 +325,7 @@ const (

Attr_PIInstanceSharedProcessorPool = "shared_processor_pool"
Attr_PIInstanceSharedProcessorPoolID = "shared_processor_pool_id"
Arg_PIInstanceSharedProcessorPool = "pi_shared_processor_pool"

// Placement Group
PIPlacementGroupID = "placement_group_id"
Expand Down Expand Up @@ -411,6 +411,5 @@ const (
PIWorkspaceDatacenter = "pi_datacenter"
PIWorkspaceResourceGroup = "pi_resource_group_id"
PIWorkspacePlan = "pi_plan"

PIVirtualOpticalDevice = "pi_virtual_optical_device"
PIVirtualOpticalDevice = "pi_virtual_optical_device"
)
Loading
Loading