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

Add dhcp network support for stratos #5503

Merged
merged 13 commits into from
Jul 15, 2024
4 changes: 4 additions & 0 deletions ibm/service/power/ibm_pi_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ const (
Dedicated = "dedicated"
DeploymentTypeEpic = "EPIC"
DeploymentTypeVMNoStorage = "VMNoStorage"
DHCPVlan = "dhcp-vlan"
Hana = "Hana"
Hard = "hard"
Host = "host"
Expand All @@ -414,10 +415,12 @@ const (
Prefix = "prefix"
Private = "private"
Public = "public"
PubVlan = "pub-vlan"
SAP = "SAP"
Shared = "shared"
Soft = "soft"
Suffix = "suffix"
Vlan = "vlan"
vSCSI = "vSCSI"
Warning = "WARNING"

Expand All @@ -444,6 +447,7 @@ const (
State_Down = "down"
State_Error = "error"
State_Failed = "failed"
State_Found = "Found"
State_Inactive = "inactive"
State_InProgress = "in progress"
State_InUse = "in-use"
Expand Down
51 changes: 42 additions & 9 deletions ibm/service/power/resource_ibm_pi_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/apparentlymart/go-cidr/cidr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

st "github.com/IBM-Cloud/power-go-client/clients/instance"
Expand Down Expand Up @@ -47,7 +48,7 @@ func ResourceIBMPINetwork() *schema.Resource {
helpers.PINetworkType: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.ValidateAllowedStringValues([]string{"vlan", "pub-vlan"}),
ValidateFunc: validate.ValidateAllowedStringValues([]string{DHCPVlan, PubVlan, Vlan}),
Description: "PI network type",
},
helpers.PINetworkName: {
Expand All @@ -73,6 +74,7 @@ func ResourceIBMPINetwork() *schema.Resource {
Optional: true,
Computed: true,
Description: "PI network gateway",
ForceNew: true,
},
helpers.PINetworkJumbo: {
Type: schema.TypeBool,
Expand Down Expand Up @@ -169,7 +171,7 @@ func resourceIBMPINetworkCreate(ctx context.Context, d *schema.ResourceData, met
body.AccessConfig = models.AccessConfig(v.(string))
}

if networktype == "vlan" {
if networktype == DHCPVlan || networktype == Vlan {
var networkcidr string
var ipBodyRanges []*models.IPAddressRange
if v, ok := d.GetOk(helpers.PINetworkCidr); ok {
Expand Down Expand Up @@ -257,7 +259,6 @@ func resourceIBMPINetworkRead(ctx context.Context, d *schema.ResourceData, meta
d.Set(helpers.PINetworkIPAddressRange, ipRangesMap)

return nil

}

func resourceIBMPINetworkUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand All @@ -271,14 +272,18 @@ func resourceIBMPINetworkUpdate(ctx context.Context, d *schema.ResourceData, met
return diag.FromErr(err)
}

if d.HasChanges(helpers.PINetworkName, helpers.PINetworkDNS, helpers.PINetworkGateway, helpers.PINetworkIPAddressRange) {
if d.HasChanges(helpers.PINetworkName, helpers.PINetworkDNS, helpers.PINetworkIPAddressRange) {
networkC := st.NewIBMPINetworkClient(ctx, sess, cloudInstanceID)
body := &models.NetworkUpdate{
DNSServers: flex.ExpandStringList((d.Get(helpers.PINetworkDNS).(*schema.Set)).List()),
}
if d.Get(helpers.PINetworkType).(string) == "vlan" {
body.Gateway = flex.PtrToString(d.Get(helpers.PINetworkGateway).(string))
body.IPAddressRanges = getIPAddressRanges(d.Get(helpers.PINetworkIPAddressRange).([]interface{}))
networkType := d.Get(helpers.PINetworkType).(string)
if d.HasChange(helpers.PINetworkIPAddressRange) {
if networkType == Vlan {
body.IPAddressRanges = getIPAddressRanges(d.Get(helpers.PINetworkIPAddressRange).([]interface{}))
} else {
return diag.Errorf("%v type does not allow ip-address range update", networkType)
}
}

if d.HasChange(helpers.PINetworkName) {
Expand Down Expand Up @@ -307,12 +312,17 @@ func resourceIBMPINetworkDelete(ctx context.Context, d *schema.ResourceData, met
return diag.FromErr(err)
}

networkC := st.NewIBMPINetworkClient(ctx, sess, cloudInstanceID)
err = networkC.Delete(networkID)
client := st.NewIBMPINetworkClient(ctx, sess, cloudInstanceID)
err = client.Delete(networkID)
if err != nil {
return diag.FromErr(err)
}

_, err = isWaitForIBMPINetworkDeleted(ctx, client, networkID, d.Timeout(schema.TimeoutDelete))
if err != nil {
return diag.FromErr(err)
}

d.SetId("")
return nil
}
Expand Down Expand Up @@ -345,6 +355,29 @@ func isIBMPINetworkRefreshFunc(client *st.IBMPINetworkClient, id string) resourc
}
}

func isWaitForIBMPINetworkDeleted(ctx context.Context, client *st.IBMPINetworkClient, id string, timeout time.Duration) (interface{}, error) {
stateConf := &retry.StateChangeConf{
Pending: []string{State_Found},
Target: []string{State_NotFound},
Refresh: isIBMPINetworkRefreshDeleteFunc(client, id),
Timeout: timeout,
Delay: 10 * time.Second,
MinTimeout: 10 * time.Second,
}

return stateConf.WaitForStateContext(ctx)
}

func isIBMPINetworkRefreshDeleteFunc(client *st.IBMPINetworkClient, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
network, err := client.Get(id)
if err != nil {
return network, State_NotFound, nil
}
return network, State_Found, nil
}
}

func generateIPData(cdir string) (gway, firstip, lastip string, err error) {
_, ipv4Net, err := net.ParseCIDR(cdir)

Expand Down
68 changes: 62 additions & 6 deletions ibm/service/power/resource_ibm_pi_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,39 @@ func TestAccIBMPINetworkGatewaybasicSatellite(t *testing.T) {
})
}

func testAccCheckIBMPINetworkDestroy(s *terraform.State) error {
func TestAccIBMPINetworkDHCPbasic(t *testing.T) {
name := fmt.Sprintf("tf-pi-network-%d", acctest.RandIntRange(10, 100))
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
CheckDestroy: testAccCheckIBMPINetworkDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMPINetworDHCPConfig(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckIBMPINetworkExists("ibm_pi_network.power_networks"),
resource.TestCheckResourceAttr(
"ibm_pi_network.power_networks", "pi_network_name", name),
resource.TestCheckResourceAttrSet("ibm_pi_network.power_networks", "pi_gateway"),
resource.TestCheckResourceAttrSet("ibm_pi_network.power_networks", "id"),
resource.TestCheckResourceAttrSet("ibm_pi_network.power_networks", "pi_ipaddress_range.#"),
),
},
{
Config: testAccCheckIBMPINetworkConfigGatewayDHCPUpdateDNS(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckIBMPINetworkExists("ibm_pi_network.power_networks"),
resource.TestCheckResourceAttr(
"ibm_pi_network.power_networks", "pi_network_name", name),
resource.TestCheckResourceAttr(
"ibm_pi_network.power_networks", "pi_dns.#", "1"),
),
},
},
})
}

func testAccCheckIBMPINetworkDestroy(s *terraform.State) error {
sess, err := acc.TestAccProvider.Meta().(conns.ClientSession).IBMPISession()
if err != nil {
return err
Expand All @@ -148,6 +179,7 @@ func testAccCheckIBMPINetworkDestroy(s *terraform.State) error {

return nil
}

func testAccCheckIBMPINetworkExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {

Expand Down Expand Up @@ -211,6 +243,19 @@ func testAccCheckIBMPINetworkGatewayConfig(name string) string {
`, acc.Pi_cloud_instance_id, name)
}

func testAccCheckIBMPINetworkGatewayConfigSatellite(name string) string {
return fmt.Sprintf(`
resource "ibm_pi_network" "power_networks" {
pi_cloud_instance_id = "%s"
pi_network_name = "%s"
pi_network_type = "vlan"
pi_cidr = "192.168.17.0/24"
pi_network_mtu = 6500
pi_network_access_config = "outbound-only"
}
`, acc.Pi_cloud_instance_id, name)
}

func testAccCheckIBMPINetworkConfigGatewayUpdateDNS(name string) string {
return fmt.Sprintf(`
resource "ibm_pi_network" "power_networks" {
Expand All @@ -228,15 +273,26 @@ func testAccCheckIBMPINetworkConfigGatewayUpdateDNS(name string) string {
`, acc.Pi_cloud_instance_id, name)
}

func testAccCheckIBMPINetworkGatewayConfigSatellite(name string) string {
func testAccCheckIBMPINetworDHCPConfig(name string) string {
return fmt.Sprintf(`
resource "ibm_pi_network" "power_networks" {
pi_cloud_instance_id = "%s"
pi_network_name = "%s"
pi_network_type = "vlan"
pi_cidr = "192.168.17.0/24"
pi_network_mtu = 6500
pi_network_access_config = "outbound-only"
pi_network_type = "dhcp-vlan"
pi_cidr = "10.1.2.0/26"
pi_dns = ["10.1.0.68"]
}
`, acc.Pi_cloud_instance_id, name)
}

func testAccCheckIBMPINetworkConfigGatewayDHCPUpdateDNS(name string) string {
return fmt.Sprintf(`
resource "ibm_pi_network" "power_networks" {
pi_cloud_instance_id = "%s"
pi_network_name = "%s"
pi_network_type = "dhcp-vlan"
pi_cidr = "10.1.2.0/26"
pi_dns = ["10.1.0.69"]
}
`, acc.Pi_cloud_instance_id, name)
}
10 changes: 5 additions & 5 deletions website/docs/r/pi_network.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ resource "ibm_pi_network" "power_networks" {
}
```

**Note**
### 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:
Expand Down Expand Up @@ -61,7 +61,7 @@ Review the argument references that you can specify for your resource.

- `pi_cloud_instance_id` - (Required, String) The GUID of the service instance associated with an account.
- `pi_network_name` - (Required, String) The name of the network.
- `pi_network_type` - (Required, String) The type of network that you want to create, such as `pub-vlan` or `vlan`.
- `pi_network_type` - (Required, String) The type of network that you want to create. Valid values are `pub-vlan`, `vlan` and `dhcp-vlan`.
- `pi_dns` - (Optional, Set of String) The DNS Servers for the network. If not specified, default is 127.0.0.1 for 'vlan' (private network) and 9.9.9.9 for 'pub-vlan' (public network). A maximum of one DNS server can be specified for private networks in Power Edge Router workspaces.
- `pi_cidr` - (Optional, String) The network CIDR. Required for `vlan` network type.
- `pi_gateway` - (Optional, String) The gateway ip address.
Expand All @@ -85,8 +85,8 @@ In addition to all argument reference list, you can access the following attribu

The `ibm_pi_network` resource can be imported by using `power_instance_id` and `network_id`.

**Example**
## Example

```
$ terraform import ibm_pi_network.example d7bec597-4726-451f-8a63-e62e6f19c32c/cea6651a-bc0a-4438-9f8a-a0770bbf3ebb
```bash
terraform import ibm_pi_network.example d7bec597-4726-451f-8a63-e62e6f19c32c/cea6651a-bc0a-4438-9f8a-a0770bbf3ebb
```
Loading