Skip to content

Commit

Permalink
Network Peers (IBM-Cloud#5835)
Browse files Browse the repository at this point in the history
Add [D]: pi_network_peers
Update [D]: pi_network, pi_networks
Update [R]: pi_network
Add require option

Add v1.9.0

fix spacing in cst file

Co-authored-by: michaelkad <michaelkadiayi@gmail.com>
  • Loading branch information
2 people authored and ismirlia committed Dec 4, 2024
1 parent e7e585f commit c13a21e
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 7 deletions.
1 change: 1 addition & 0 deletions ibm/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ func Provider() *schema.Provider {
"ibm_pi_network_address_groups": power.DataSourceIBMPINetworkAddressGroups(),
"ibm_pi_network_interface": power.DataSourceIBMPINetworkInterface(),
"ibm_pi_network_interfaces": power.DataSourceIBMPINetworkInterfaces(),
"ibm_pi_network_peers": power.DataSourceIBMPINetworkPeers(),
"ibm_pi_network_port": power.DataSourceIBMPINetworkPort(),
"ibm_pi_network_security_group": power.DataSourceIBMPINetworkSecurityGroup(),
"ibm_pi_network_security_groups": power.DataSourceIBMPINetworkSecurityGroups(),
Expand Down
29 changes: 28 additions & 1 deletion ibm/service/power/data_source_ibm_pi_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func DataSourceIBMPINetwork() *schema.Resource {
// Attributes
Attr_AccessConfig: {
Computed: true,
Description: "The network communication configuration option of the network (for satellite locations only).",
Deprecated: "This field is deprecated please use peer_id instead.",
Description: "The network communication configuration option of the network (for on prem locations only). Use `peer_id` instead.",
Type: schema.TypeString,
},
Attr_AvailableIPCount: {
Expand Down Expand Up @@ -83,6 +84,25 @@ func DataSourceIBMPINetwork() *schema.Resource {
Description: "The unique identifier or name of a network.",
Type: schema.TypeString,
},
Attr_NetworkAddressTranslation: {
Computed: true,
Description: "Contains the network address translation details (for on prem locations only).",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Attr_SourceIP: {
Computed: true,
Description: "source IP address.",
Type: schema.TypeString,
},
},
},
Type: schema.TypeList,
},
Attr_PeerID: {
Computed: true,
Description: "Network peer ID (for on prem locations only).",
Type: schema.TypeString,
},
Attr_Type: {
Computed: true,
Description: "The type of network.",
Expand Down Expand Up @@ -153,6 +173,13 @@ func dataSourceIBMPINetworkRead(ctx context.Context, d *schema.ResourceData, met
if networkdata.Name != nil {
d.Set(Attr_Name, networkdata.Name)
}
networkAddressTranslation := []map[string]interface{}{}
if networkdata.NetworkAddressTranslation != nil {
natMap := networkAddressTranslationToMap(networkdata.NetworkAddressTranslation)
networkAddressTranslation = append(networkAddressTranslation, natMap)
}
d.Set(Attr_NetworkAddressTranslation, networkAddressTranslation)
d.Set(Attr_PeerID, networkdata.PeerID)
if networkdata.Type != nil {
d.Set(Attr_Type, networkdata.Type)
}
Expand Down
109 changes: 109 additions & 0 deletions ibm/service/power/data_source_ibm_pi_network_peers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright IBM Corp. 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package power

import (
"context"

"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"

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

func DataSourceIBMPINetworkPeers() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMPINetworkPeersRead,

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

// Attributes
Attr_NetworkPeers: {
Computed: true,
Description: "List of network peers.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Attr_Description: {
Computed: true,
Description: "Description of the network peer.",
Type: schema.TypeString,
},
Attr_ID: {
Computed: true,
Description: "ID of the network peer.",
Type: schema.TypeString,
},
Attr_Name: {
Computed: true,
Description: "Name of the network peer.",
Type: schema.TypeString,
},
Attr_Type: {
Computed: true,
Description: "Type of the network peer.",
Type: schema.TypeString,
},
},
},
Type: schema.TypeList,
},
},
}
}

func dataSourceIBMPINetworkPeersRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)

networkC := instance.NewIBMPINetworkPeerClient(ctx, sess, cloudInstanceID)
networkdata, err := networkC.GetNetworkPeers()
if err != nil {
return diag.FromErr(err)
}
var clientgenU, _ = uuid.GenerateUUID()
d.SetId(clientgenU)

networkPeers := []map[string]interface{}{}
if networkdata.NetworkPeers != nil {
for _, np := range networkdata.NetworkPeers {
npMap := dataSourceIBMPINetworkPeersNetworkPeerToMap(np)

networkPeers = append(networkPeers, npMap)
}
}
d.Set(Attr_NetworkPeers, networkPeers)

return nil
}

func dataSourceIBMPINetworkPeersNetworkPeerToMap(np *models.NetworkPeer) map[string]interface{} {
npMap := make(map[string]interface{})
if np.Description != nil {
npMap[Attr_Description] = np.Description
}
if np.ID != nil {
npMap[Attr_ID] = np.ID
}
if np.Name != nil {
npMap[Attr_Name] = np.Name
}
if np.Type != nil {
npMap[Attr_Type] = np.Type
}
return npMap
}
37 changes: 37 additions & 0 deletions ibm/service/power/data_source_ibm_pi_network_peers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright IBM Corp. 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package power_test

import (
"fmt"
"testing"

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

acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest"
)

func TestAccIBMPINetworkPeersDataSourceBasic(t *testing.T) {
networksResData := "data.ibm_pi_network_peers.network_peers"
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMPINetworkPeersDataSourceConfigBasic(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(networksResData, "id"),
resource.TestCheckResourceAttrSet(networksResData, "network_peers.#"),
),
},
},
})
}

func testAccCheckIBMPINetworkPeersDataSourceConfigBasic() string {
return fmt.Sprintf(`
data "ibm_pi_network_peers" "network_peers" {
pi_cloud_instance_id = "%s"
}`, acc.Pi_cloud_instance_id)
}
9 changes: 8 additions & 1 deletion ibm/service/power/data_source_ibm_pi_networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func DataSourceIBMPINetworks() *schema.Resource {
Schema: map[string]*schema.Schema{
Attr_AccessConfig: {
Computed: true,
Description: "The network communication configuration option of the network (for satellite locations only).",
Deprecated: "This field is deprecated please use peer_id instead.",
Description: "The network communication configuration option of the network (for on-prem locations only). Use `peer_id` instead.",
Type: schema.TypeString,
},
Attr_CRN: {
Expand Down Expand Up @@ -70,6 +71,11 @@ func DataSourceIBMPINetworks() *schema.Resource {
Description: "The unique identifier of a network.",
Type: schema.TypeString,
},
Attr_PeerID: {
Computed: true,
Description: "Network Peer ID.",
Type: schema.TypeString,
},
Attr_Type: {
Computed: true,
Description: "The type of network.",
Expand Down Expand Up @@ -126,6 +132,7 @@ func flattenNetworks(list []*models.NetworkReference, meta interface{}) []map[st
Attr_MTU: i.Mtu,
Attr_Name: *i.Name,
Attr_NetworkID: *i.NetworkID,
Attr_PeerID: i.PeerID,
Attr_Type: *i.Type,
Attr_VLanID: *i.VlanID,
}
Expand Down
3 changes: 1 addition & 2 deletions ibm/service/power/resource_ibm_pi_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ func resourceIBMPINetworkCreate(ctx context.Context, d *schema.ResourceData, met
}
}
}

networkResponse, err := client.Create(body)
if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -601,7 +602,6 @@ func isPERWorkspaceRefreshFunc(client *instance.IBMPIWorkspacesClient, id string
return ws, State_Configuring, nil
}
}

func networkMapToNetworkCreatePeer(networkCreatePeerMap map[string]interface{}) *models.NetworkCreatePeer {
ncp := &models.NetworkCreatePeer{}
if networkCreatePeerMap[Attr_ID].(string) != "" {
Expand All @@ -617,7 +617,6 @@ func networkMapToNetworkCreatePeer(networkCreatePeerMap map[string]interface{})
}
return ncp
}

func natMapToNetworkAddressTranslation(networkAddressTranslationMap map[string]interface{}) *models.NetworkAddressTranslation {
nat := &models.NetworkAddressTranslation{}
if networkAddressTranslationMap[Attr_SourceIP].(string) != "" {
Expand Down
37 changes: 36 additions & 1 deletion ibm/service/power/resource_ibm_pi_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,26 @@ func TestAccIBMPINetworkUserTags(t *testing.T) {
},
})
}

func TestAccIBMPINetworkPeerOnPrem(t *testing.T) {
name := fmt.Sprintf("tf-pi-network-%d", acctest.RandIntRange(10, 100))
networkRes := "ibm_pi_network.power_network_peer"
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
CheckDestroy: testAccCheckIBMPINetworkDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMPINetworkPeerOnPrem(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckIBMPINetworkExists(networkRes),
resource.TestCheckResourceAttr(networkRes, "pi_network_name", name),
resource.TestCheckResourceAttrSet(networkRes, "id"),
resource.TestCheckResourceAttrSet(networkRes, "peer_id"),
),
},
},
})
}
func testAccCheckIBMPINetworkDestroy(s *terraform.State) error {
sess, err := acc.TestAccProvider.Meta().(conns.ClientSession).IBMPISession()
if err != nil {
Expand Down Expand Up @@ -340,3 +359,19 @@ func testAccCheckIBMPINetworkUserTagsConfig(name string, userTagsString string)
}
`, acc.Pi_cloud_instance_id, name, userTagsString)
}

func testAccCheckIBMPINetworkPeerOnPrem(name string) string {
return fmt.Sprintf(`
resource "ibm_pi_network" "power_network_peer" {
pi_cloud_instance_id = "%s"
pi_network_name = "%s"
pi_network_type = "vlan"
pi_cidr = "192.168.17.0/24"
pi_network_peer {
id = "2"
type = "L2"
}
}
`, acc.Pi_cloud_instance_id, name)
}
54 changes: 54 additions & 0 deletions website/docs/d/pi_network_peers.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
subcategory: "Power Systems"
layout: "ibm"
page_title: "IBM : ibm_pi_network_peers"
description: |-
Get information about IBM Power Virtual Server cloud network peers.
---

# ibm_pi_network_peers

Provides a read-only data source to retrieve information about pi_network_peers for on-prem locations.

## Example Usage

```terraform
data "ibm_pi_network_peers" "pi_network_peers" {
pi_cloud_instance_id = "49fba6c9-23f8-40bc-9899-aca322ee7d5b"
}
```

### 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) The GUID of the service instance associated with an account.

## 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 unique identifier of the pi_network_peers.
- `network_peers` - (List) List of network peers.

Nested schema for `network_peers`:
- `description` - (String) Description of the network peer.
- `id` - (String) ID of the network peer.
- `name` - (String) Name of the network peer.
- `type` - (String) Type of the network peer.
3 changes: 2 additions & 1 deletion website/docs/d/pi_networks.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ In addition to all argument reference list, you can access the following attribu
- `networks` - (List) List of all networks.

Nested scheme for `networks`:
- `access_config` - (String) The network communication configuration option of the network (for satellite locations only).
- `access_config` - (Deprecated, String) The network communication configuration option of the network (for on-prem locations only). Use `peer_id` instead.
- `crn` - (String) The CRN of this resource.
- `dhcp_managed` - (Boolean) Indicates if the network DHCP Managed.
- `href` - (String) The hyper link of a network.
- `mtu` - (Boolean) Maximum Transmission Unit option of the network.
- `name` - (String) The name of a network.
- `network_id` - (String) The ID of the network.
- `peer_id` - (String) Network peer ID (for on-prem locations only).
- `type` - (String) The type of network.
- `user_tags` - (List) List of user tags attached to the resource.
- `vlan_id` - (String) The VLAN ID that the network is connected to.
1 change: 0 additions & 1 deletion website/docs/r/pi_network.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ Review the argument references that you can specify for your resource.
Nested schema for `pi_network_peer`:
- `id` - (Required, String) ID of the network peer.
- `network_address_translation` - (Optional, List) Contains the Network Address Translation Details. Max items: 1.

Nested schema for `network_address_translation`:
- `source_ip` - (Optional, String) source IP address, required if network peer type is `L3BGP` or `L3STATIC` and if NAT is enabled.
- `type` - (Optional, String) Type of the network peer. Allowable values are: `L2`, `L3BGP`, `L3Static`.
Expand Down

0 comments on commit c13a21e

Please sign in to comment.