Skip to content

Commit

Permalink
renamed pi_ipaddress_range params
Browse files Browse the repository at this point in the history
  • Loading branch information
dhirendersingh19 committed Mar 1, 2022
1 parent f42ba58 commit bae8377
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 41 deletions.
58 changes: 28 additions & 30 deletions ibm/service/power/resource_ibm_pi_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ import (
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
)

const (
piEndingIPAaddress = "pi_ending_ip_address"
piStartingIPAaddress = "pi_starting_ip_address"
)

func ResourceIBMPINetwork() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIBMPINetworkCreate,
Expand Down Expand Up @@ -86,12 +91,12 @@ func ResourceIBMPINetwork() *schema.Resource {
Description: "List of one or more ip address range(s)",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ending_ip_address": {
piEndingIPAaddress: {
Type: schema.TypeString,
Required: true,
Description: "Ending ip address",
},
"starting_ip_address": {
piStartingIPAaddress: {
Type: schema.TypeString,
Required: true,
Description: "Starting ip address",
Expand Down Expand Up @@ -123,19 +128,22 @@ func resourceIBMPINetworkCreate(ctx context.Context, d *schema.ResourceData, met
cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
networkname := d.Get(helpers.PINetworkName).(string)
networktype := d.Get(helpers.PINetworkType).(string)
networkdns := flex.ExpandStringList((d.Get(helpers.PINetworkDNS).(*schema.Set)).List())

client := st.NewIBMPINetworkClient(ctx, sess, cloudInstanceID)
var body = &models.NetworkCreate{
Type: &networktype,
Name: networkname,
}
if v, ok := d.GetOk(helpers.PINetworkDNS); ok {
networkdns := flex.ExpandStringList((v.(*schema.Set)).List())
if len(networkdns) > 0 {
body.DNSServers = networkdns
}
}

if v, ok := d.GetOk(helpers.PINetworkJumbo); ok {
body.Jumbo = v.(bool)
}
if len(networkdns) > 0 {
body.DNSServers = networkdns
}

if networktype == "vlan" {
var networkcidr, gateway, firstip, lastip string
Expand All @@ -146,6 +154,13 @@ func resourceIBMPINetworkCreate(ctx context.Context, d *schema.ResourceData, met
return diag.Errorf("%s is required when %s is vlan", helpers.PINetworkCidr, helpers.PINetworkType)
}

gateway, firstip, lastip, err = generateIPData(networkcidr)
if err != nil {
return diag.FromErr(err)
}

ipBodyRanges = []*models.IPAddressRange{{EndingIPAddress: &lastip, StartingIPAddress: &firstip}}

if g, ok := d.GetOk(helpers.PINetworkGateway); ok {
gateway = g.(string)
}
Expand All @@ -154,23 +169,6 @@ func resourceIBMPINetworkCreate(ctx context.Context, d *schema.ResourceData, met
ipBodyRanges = getIPAddressRanges(ips.([]interface{}))
}

if len(gateway) == 0 {
log.Printf("[INFO] %v is empty, calacultaing %v", helpers.PINetworkGateway, helpers.PINetworkGateway)
gateway, _, _, err = generateIPData(networkcidr)
if err != nil {
return diag.FromErr(err)
}
}

if len(ipBodyRanges) == 0 {
log.Printf("[INFO] %v is empty, calacultaing %v", helpers.PINetworkIPAddressRange, helpers.PINetworkIPAddressRange)
_, firstip, lastip, err = generateIPData(networkcidr)
if err != nil {
return diag.FromErr(err)
}
ipBodyRanges = []*models.IPAddressRange{{EndingIPAddress: &lastip, StartingIPAddress: &firstip}}
}

body.IPAddressRanges = ipBodyRanges
body.Gateway = gateway
body.Cidr = networkcidr
Expand All @@ -181,11 +179,11 @@ func resourceIBMPINetworkCreate(ctx context.Context, d *schema.ResourceData, met
return diag.FromErr(err)
}

IBMPINetworkID := *networkResponse.NetworkID
networkID := *networkResponse.NetworkID

d.SetId(fmt.Sprintf("%s/%s", cloudInstanceID, IBMPINetworkID))
d.SetId(fmt.Sprintf("%s/%s", cloudInstanceID, networkID))

_, err = isWaitForIBMPINetworkAvailable(ctx, client, IBMPINetworkID, d.Timeout(schema.TimeoutCreate))
_, err = isWaitForIBMPINetworkAvailable(ctx, client, networkID, d.Timeout(schema.TimeoutCreate))
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -223,8 +221,8 @@ func resourceIBMPINetworkRead(ctx context.Context, d *schema.ResourceData, meta
for _, n := range networkdata.IPAddressRanges {
if n != nil {
v := map[string]interface{}{
"ending_ip_address": n.EndingIPAddress,
"starting_ip_address": n.StartingIPAddress,
piEndingIPAaddress: n.EndingIPAddress,
piStartingIPAaddress: n.StartingIPAddress,
}
ipRangesMap = append(ipRangesMap, v)
}
Expand Down Expand Up @@ -373,8 +371,8 @@ func getIPAddressRanges(ipAddressRanges []interface{}) []*models.IPAddressRange
if v != nil {
ipAddressRange := v.(map[string]interface{})
ipRange := &models.IPAddressRange{
EndingIPAddress: flex.PtrToString(ipAddressRange["ending_ip_address"].(string)),
StartingIPAddress: flex.PtrToString(ipAddressRange["starting_ip_address"].(string)),
EndingIPAddress: flex.PtrToString(ipAddressRange[piEndingIPAaddress].(string)),
StartingIPAddress: flex.PtrToString(ipAddressRange[piStartingIPAaddress].(string)),
}
ipRanges = append(ipRanges, ipRange)
}
Expand Down
14 changes: 10 additions & 4 deletions ibm/service/power/resource_ibm_pi_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func TestAccIBMPINetworkbasic(t *testing.T) {
testAccCheckIBMPINetworkExists("ibm_pi_network.power_networks"),
resource.TestCheckResourceAttr(
"ibm_pi_network.power_networks", "pi_network_name", name),
resource.TestCheckResourceAttrSet("ibm_pi_network.power_networks", "id"),
resource.TestCheckResourceAttrSet("ibm_pi_network.power_networks", "pi_gateway"),
resource.TestCheckResourceAttrSet("ibm_pi_network.power_networks", "pi_ipaddress_range.#"),
),
},
{
Expand All @@ -42,6 +45,9 @@ func TestAccIBMPINetworkbasic(t *testing.T) {
"ibm_pi_network.power_networks", "pi_network_name", name),
resource.TestCheckResourceAttr(
"ibm_pi_network.power_networks", "pi_dns.#", "1"),
resource.TestCheckResourceAttrSet("ibm_pi_network.power_networks", "id"),
resource.TestCheckResourceAttrSet("ibm_pi_network.power_networks", "pi_gateway"),
resource.TestCheckResourceAttrSet("ibm_pi_network.power_networks", "pi_ipaddress_range.#"),
),
},
},
Expand Down Expand Up @@ -74,9 +80,9 @@ func TestAccIBMPINetworkGatewaybasic(t *testing.T) {
resource.TestCheckResourceAttr(
"ibm_pi_network.power_networks", "pi_gateway", "192.168.17.2"),
resource.TestCheckResourceAttr(
"ibm_pi_network.power_networks", "pi_ipaddress_range.0.ending_ip_address", "192.168.17.254"),
"ibm_pi_network.power_networks", "pi_ipaddress_range.0.pi_ending_ip_address", "192.168.17.254"),
resource.TestCheckResourceAttr(
"ibm_pi_network.power_networks", "pi_ipaddress_range.0.starting_ip_address", "192.168.17.3"),
"ibm_pi_network.power_networks", "pi_ipaddress_range.0.pi_starting_ip_address", "192.168.17.3"),
),
},
},
Expand Down Expand Up @@ -178,8 +184,8 @@ func testAccCheckIBMPINetworkConfigGatewayUpdateDNS(name string) string {
pi_gateway = "192.168.17.2"
pi_cidr = "192.168.17.0/24"
pi_ipaddress_range {
ending_ip_address = "192.168.17.254"
starting_ip_address = "192.168.17.3"
pi_ending_ip_address = "192.168.17.254"
pi_starting_ip_address = "192.168.17.3"
}
}
`, acc.Pi_cloud_instance_id, name)
Expand Down
13 changes: 6 additions & 7 deletions website/docs/r/pi_network.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ resource "ibm_pi_network" "power_networks" {
pi_dns = [<"DNS Servers">]
pi_gateway = "192.168.0.1"
pi_ipaddress_range {
starting_ip_address = "192.168.0.2"
ending_ip_address = "192.168.0.254"
pi_starting_ip_address = "192.168.0.2"
pi_ending_ip_address = "192.168.0.254"
}
}
```
Expand Down Expand Up @@ -61,13 +61,12 @@ Review the argument references that you can specify for your resource.
- `pi_dns` - (Optional, Set of String) The DNS Servers for the network. Required for `vlan` network type.
- `pi_cidr` - (Optional, String) The network CIDR. Required for `vlan` network type.
- `pi_gateway` - (Optional, String) The gateway ip address.
- `pi_ipaddress_range` - (Optional, List of Map) List of one or more ip address range. The `pi_ipaddress_range` object structure is documented below. **Note** if the `pi_gateway` or `pi_ipaddress_range` is not provided, it will calculate the value based on CIDR respectively.
- `pi_ipaddress_range` - (Optional, List of Map) List of one or more ip address range. The `pi_ipaddress_range` object structure is documented below.
The `pi_ipaddress_range` block supports:
- `pi_ending_ip_address` - (Required, String) The ending ip address.
- `pi_starting_ip_address` - (Required, String) The staring ip address. **Note** if the `pi_gateway` or `pi_ipaddress_range` is not provided, it will calculate the value based on CIDR respectively.
- `pi_network_jumbo` - (Optional, Bool) MTU Jumbo option of the network.

The `pi_ipaddress_range` block supports:
- `ending_ip_address` - (Required, String) The ending ip address.
- `starting_ip_address` - (Required, String) The staring ip address.

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

Expand Down

0 comments on commit bae8377

Please sign in to comment.