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 #27

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
73 changes: 31 additions & 42 deletions ibm/service/power/data_source_ibm_pi_dhcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,119 +6,108 @@ 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",
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_DhcpNetworkDeprecated: {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the DHCP Server private network (deprecated - replaced by network_id)",
},
Attr_DhcpNetworkID: {
Description: "ID of the DHCP Server private network (deprecated - replaced by network_id)",
Type: schema.TypeString,
Computed: true,
Description: "The ID of the DHCP Server private network",
},
Attr_DhcpNetworkName: {
Type: schema.TypeString,
Attr_NetworkID: {
Computed: true,
Description: "The name of the DHCP Server private network",
Description: "ID of the DHCP Server private network.",
Type: schema.TypeString,
},
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_DhcpNetworkDeprecated, *dhcpNetwork.ID)
d.Set(Attr_DhcpNetworkID, *dhcpNetwork.ID)
d.Set(Attr_NetworkID, *dhcpNetwork.ID)
}
if dhcpNetwork.Name != nil {
d.Set(Attr_DhcpNetworkName, *dhcpNetwork.Name)
d.Set(Attr_NetworkName, *dhcpNetwork.Name)
}
}

Expand All @@ -130,7 +119,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
63 changes: 25 additions & 38 deletions ibm/service/power/data_source_ibm_pi_dhcps.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,107 +7,94 @@ 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_DhcpNetworkDeprecated: {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the DHCP Server private network (deprecated - replaced by network_id)",
},
Attr_DhcpNetworkID: {
Description: "ID of the DHCP Server private network (deprecated - replaced by network_id)",
Type: schema.TypeString,
Computed: true,
Description: "The ID of the DHCP Server private network",
},
Attr_DhcpNetworkName: {
Type: schema.TypeString,
Attr_NetworkID: {
Computed: true,
Description: "The name of the DHCP Server private network",
Description: "ID of the DHCP Server private network.",
Type: schema.TypeString,
},
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_DhcpNetworkDeprecated, *dhcpNetwork.ID)
d.Set(Attr_DhcpNetworkID, *dhcpNetwork.ID)
d.Set(Attr_NetworkID, *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
3 changes: 3 additions & 0 deletions ibm/service/power/ibm_pi_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const (
Attr_KeyCreationDate = "creation_date"
Attr_Key = "ssh_key"
Attr_KeyName = "name"
Attr_Leases = "leases"
Attr_NetworkID = "network_id"
Attr_NetworkName = "network_name"

// SAP Profile
PISAPProfiles = "profiles"
Expand Down
53 changes: 24 additions & 29 deletions website/docs/d/pi_dhcp.html.markdown
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---

subcategory: "Power Systems"
layout: "ibm"
page_title: "IBM: pi_dhcp"
Expand All @@ -8,49 +7,45 @@ description: |-
---

# ibm_pi_dhcp

Retrieve information about a DHCP Server. For more information, see [getting started with IBM Power Systems Virtual Servers](https://cloud.ibm.com/docs/power-iaas?topic=power-iaas-getting-started).

## Example usage

```terraform
data "ibm_pi_dhcp" "example" {
pi_cloud_instance_id = "<value of the cloud_instance_id>"
pi_dhcp_id = "0e48e1be-9f54-4a67-ba55-7e31ce98b65a"
}
```

**Notes**
- Please find [supported Regions](https://cloud.ibm.com/apidocs/power-cloud#endpoint) for endpoints.
- If a Power cloud instance is provisioned at `lon04`, The provider level attributes should be as follows:
- `region` - `lon`
- `zone` - `lon04`

Example usage:
```terraform
provider "ibm" {
region = "lon"
zone = "lon04"
}
```

## Argument reference
Review the argument references that you can specify for your data source.

- `pi_cloud_instance_id` - (Required, String) Cloud Instance ID of a PCloud Instance.
- `pi_dhcp_id` - (Required, String) The ID of the DHCP Server.
- `pi_cloud_instance_id` - (Required, String) The GUID of the service instance associated with an account.
- `pi_dhcp_id` - (Required, String) ID of the DHCP Server.

## Attribute reference
In addition to all argument reference list, you can access the following attribute references after your data source is created.

- `id` - (String) The ID of the DHCP Server.
- `leases` - (List) The list of DHCP Server PVM Instance leases.
- `dhcp_id` - (String) ID of the DHCP Server.
- `leases` - (List) List of DHCP Server PVM Instance leases.
Nested scheme for `leases`:
- `instance_ip` - (String) The IP of the PVM Instance.
- `instance_mac` - (String) The MAC Address of the PVM Instance.
- `network` - (String) The ID of the DHCP Server private network (deprecated - replaced by `network_id`).
- `network_id`- (String) The ID of the DHCP Server private network.
- `network_name` - The name of the DHCP Server private network.
- `status` - (String) The status of the DHCP Server.

**Note**

* Please find [supported Regions](https://cloud.ibm.com/apidocs/power-cloud#endpoint) for endpoints.
* If a Power cloud instance is provisioned at `lon04`, The provider level attributes should be as follows:
* `region` - `lon`
* `zone` - `lon04`

Example usage:

```terraform
provider "ibm" {
region = "lon"
zone = "lon04"
}
```
- `instance_ip` - (String) IP of the PVM Instance.
- `instance_mac` - (String) MAC Address of the PVM Instance.
- `network` - (String) ID of the DHCP Server private network (deprecated - replaced by `network_id`).
- `network_id`- (String) ID of the DHCP Server private network.
- `network_name` - (String) Name of the DHCP Server private network.
- `status` - (String) Status of the DHCP Server.
Loading
Loading