Skip to content

Commit

Permalink
[minor_change] Add aci_cloud_ipsec_tunnel_subnet_pool, aci_cloud_exte…
Browse files Browse the repository at this point in the history
…rnal_network and aci_cloud_external_network_vpn_network resources and datasources for Cloud APIC (CiscoDevNet#948)
  • Loading branch information
anvitha-jain authored Jan 19, 2023
1 parent 0150c5a commit b1ca47f
Show file tree
Hide file tree
Showing 26 changed files with 2,531 additions and 9 deletions.
109 changes: 109 additions & 0 deletions aci/data_source_aci_cloudtemplateextnetwork.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package aci

import (
"context"
"fmt"
"log"

"github.com/ciscoecosystem/aci-go-client/v2/client"
"github.com/ciscoecosystem/aci-go-client/v2/models"
"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 dataSourceAciCloudTemplateforExternalNetwork() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceAciCloudTemplateforExternalNetworkRead,
SchemaVersion: 1,
Schema: AppendBaseAttrSchema(AppendNameAliasAttrSchema(map[string]*schema.Schema{
"hub_network_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"vrf_dn": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"regions": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Computed: true,
},
"cloud_vendor": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"aws",
"azure",
"gcp",
}, false),
},
"router_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"c8kv",
"tgw",
}, false),
},
})),
}
}

func dataSourceAciCloudTemplateforExternalNetworkRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
aciClient := m.(*client.Client)
name := d.Get("name").(string)
rn := fmt.Sprintf(models.RncloudtemplateExtNetwork, name)
dn := fmt.Sprintf("%s/%s", models.CloudInfraNetworkDefaultTemplateDn, rn)
log.Printf("[DEBUG] %s: Data Source - Beginning Read", dn)

cloudtemplateExtNetwork, err := getRemoteCloudTemplateforExternalNetwork(aciClient, dn)
if err != nil {
return diag.FromErr(err)
}

d.SetId(dn)

_, err = setCloudTemplateforExternalNetworkAttributes(cloudtemplateExtNetwork, d)
if err != nil {
return diag.FromErr(err)
}

log.Printf("[DEBUG] : Data Source - Begining Read of cloud Regions attributes.")
regionsData, err := aciClient.ListCloudProviderandRegionNames(cloudtemplateExtNetwork.DistinguishedName)
if err != nil {
log.Printf("[DEBUG] : Data Source - Error while reading cloud Regions attributes %v", err)
}

regionsList := make([]string, 0, 1)
for _, regionValue := range regionsData {

regionsMap, err := setCloudProviderandRegionNamesAttributes(regionValue, make(map[string]string))
if err != nil {
d.SetId("")
return nil
}
regionsList = append(regionsList, regionsMap["region"])
d.Set("cloud_vendor", regionsMap["cloud_vendor"])
if regionsMap["cloud_vendor"] != "aws" {
d.Set("router_type", "")
}
}
log.Printf("[DEBUG] : Data Source - Read cloud regions finished successfully")
d.Set("regions", regionsList)

log.Printf("[DEBUG] %s: Data Source - Read finished successfully", dn)
return nil
}
50 changes: 50 additions & 0 deletions aci/data_source_aci_cloudtemplateipsectunnelsubnetpool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package aci

import (
"context"
"fmt"

"github.com/ciscoecosystem/aci-go-client/v2/client"
"github.com/ciscoecosystem/aci-go-client/v2/models"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceAciSubnetPoolforIpSecTunnels() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceAciSubnetPoolforIpSecTunnelsRead,
SchemaVersion: 1,
Schema: AppendBaseAttrSchema(AppendNameAliasAttrSchema(map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"subnet_pool": {
Type: schema.TypeString,
Required: true,
},
})),
}
}

func dataSourceAciSubnetPoolforIpSecTunnelsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
aciClient := m.(*client.Client)
subnetpool := d.Get("subnet_pool").(string)
rn := fmt.Sprintf(models.RncloudtemplateIpSecTunnelSubnetPool, subnetpool)
dn := fmt.Sprintf("%s/%s", models.CloudInfraNetworkDefaultTemplateDn, rn)

cloudtemplateIpSecTunnelSubnetPool, err := getRemoteSubnetPoolforIpSecTunnels(aciClient, dn)
if err != nil {
return diag.FromErr(err)
}

d.SetId(dn)

_, err = setSubnetPoolforIpSecTunnelsAttributes(cloudtemplateIpSecTunnelSubnetPool, d)
if err != nil {
return diag.FromErr(err)
}

return nil
}
154 changes: 154 additions & 0 deletions aci/data_source_aci_cloudtemplatevpnnetwork.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package aci

import (
"context"
"fmt"
"log"

"github.com/ciscoecosystem/aci-go-client/v2/client"
"github.com/ciscoecosystem/aci-go-client/v2/models"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceAciCloudTemplateforVPNNetwork() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceAciCloudTemplateforVPNNetworkRead,
SchemaVersion: 1,
Schema: AppendBaseAttrSchema(AppendNameAliasAttrSchema(map[string]*schema.Schema{
"aci_cloud_external_network_dn": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"remote_site_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"remote_site_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"ipsec_tunnel": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ike_version": {
Type: schema.TypeString,
Required: true,
},
"public_ip_address": {
Type: schema.TypeString,
Required: true,
},
"subnet_pool_name": {
Type: schema.TypeString,
Required: true,
},
"pre_shared_key": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"bgp_peer_asn": {
Type: schema.TypeString,
Required: true,
},
"source_interfaces": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Computed: true,
},
},
},
},
})),
}
}

func dataSourceAciCloudTemplateforVPNNetworkRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
aciClient := m.(*client.Client)
name := d.Get("name").(string)
TemplateforExternalNetworkDn := d.Get("aci_cloud_external_network_dn").(string)
rn := fmt.Sprintf(models.RncloudtemplateVpnNetwork, name)
dn := fmt.Sprintf("%s/%s", TemplateforExternalNetworkDn, rn)
log.Printf("[DEBUG] %s: Data Source - Beginning Read", dn)

cloudtemplateVpnNetwork, err := getRemoteTemplateforVPNNetwork(aciClient, dn)
if err != nil {
return diag.FromErr(err)
}

d.SetId(dn)

_, err = setTemplateforVPNNetworkAttributes(cloudtemplateVpnNetwork, d)
if err != nil {
return diag.FromErr(err)
}

log.Printf("[DEBUG] Data Source - Begining Read of cloud IPsec Tunnel attributes.")
cloudtemplateIpSecTunnelData, err := aciClient.ListCloudTemplateforIpSectunnel(dn)
if err != nil {
log.Printf("[DEBUG] Data Source - Error while reading cloud IPsec Tunnel attributes %v", err)
}

cloudtemplateIpSecTunnelSet := make([]map[string]interface{}, 0, 1)
for _, cloudtemplateIpSecTunnel := range cloudtemplateIpSecTunnelData {

cloudIpSecTunnelAttMap, cloudtemplateIpSecTunnelDn, err := setCloudTemplateforIpSecTunnelAttributes(cloudtemplateIpSecTunnel, make(map[string]interface{}))
if err != nil {
d.SetId("")
return nil
}

log.Printf("[DEBUG] Data Source - Begining Read of cloud BGP IPV4 Peer attributes.")
bgpIPv4PeerData, err := aciClient.ListCloudTemplateBGPIPv4Peer(cloudtemplateIpSecTunnelDn)
if err != nil {
log.Printf("[DEBUG] Data Source - Error while reading cloud BGP IPV4 Peer attributes %v", err)
}
for _, bgpIPv4Peer := range bgpIPv4PeerData {
bgpPeerAsnAtt, err := getASNfromBGPTPV4Peer(bgpIPv4Peer, make(map[string]string))
if err != nil {
d.SetId("")
return nil
}
cloudIpSecTunnelAttMap["bgp_peer_asn"] = bgpPeerAsnAtt["bgp_peer_asn_att"]
}
log.Printf("[DEBUG] Data Source - Read cloud BGP IPV4 Peer finished successfully.")

log.Printf("[DEBUG] Data Source - Begining Read of cloud IPsec Tunnel Source Interface attributes.")
ipSectunnelSourceInterfaceData, err := aciClient.ListCloudTemplateforIpSectunnelSourceInterface(cloudtemplateIpSecTunnelDn)
if err != nil {
log.Printf("[DEBUG] Data Source - Error while reading cloud IPsec Tunnel Source Interface attributes %v", err)
}

ipSectunnelSourceInterfaceList := make([]string, 0, 1)
for _, ipSecTunnelSourceInterfaceValue := range ipSectunnelSourceInterfaceData {
ipSectunnelSourceInterfaceName, err := formatTemplateforIpSectunnelAttributes(ipSecTunnelSourceInterfaceValue)
if err != nil {
d.SetId("")
return nil
}
ipSectunnelSourceInterfaceList = append(ipSectunnelSourceInterfaceList, ipSectunnelSourceInterfaceName)
}
cloudIpSecTunnelAttMap["source_interfaces"] = ipSectunnelSourceInterfaceList
log.Printf("[DEBUG] : Data Source - Read cloud IPsec Tunnel Source Interface finished successfully")

cloudtemplateIpSecTunnelSet = append(cloudtemplateIpSecTunnelSet, cloudIpSecTunnelAttMap)
}
d.Set("ipsec_tunnel", cloudtemplateIpSecTunnelSet)
log.Printf("[DEBUG] Data Source - Read cloud IPsec Tunnel finished successfully.")

log.Printf("[DEBUG] %s: Data Source - Read finished successfully", dn)
return nil
}
6 changes: 6 additions & 0 deletions aci/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ func Provider() *schema.Provider {
"aci_logical_interface_profile": resourceAciLogicalInterfaceProfile(),
"aci_l3_ext_subnet": resourceAciL3ExtSubnet(),
"aci_cloud_applicationcontainer": resourceAciCloudApplicationcontainer(),
"aci_cloud_ipsec_tunnel_subnet_pool": resourceAciSubnetPoolforIpSecTunnels(),
"aci_cloud_external_network": resourceAciCloudTemplateforExternalNetwork(),
"aci_cloud_external_network_vpn_network": resourceAciCloudTemplateforVPNNetwork(),
"aci_cloud_aws_provider": resourceAciCloudAWSProvider(),
"aci_cloud_cidr_pool": resourceAciCloudCIDRPool(),
"aci_cloud_domain_profile": resourceAciCloudDomainProfile(),
Expand Down Expand Up @@ -366,6 +369,9 @@ func Provider() *schema.Provider {
"aci_logical_interface_profile": dataSourceAciLogicalInterfaceProfile(),
"aci_l3_ext_subnet": dataSourceAciL3ExtSubnet(),
"aci_cloud_applicationcontainer": dataSourceAciCloudApplicationcontainer(),
"aci_cloud_ipsec_tunnel_subnet_pool": dataSourceAciSubnetPoolforIpSecTunnels(),
"aci_cloud_external_network": dataSourceAciCloudTemplateforExternalNetwork(),
"aci_cloud_external_network_vpn_network": dataSourceAciCloudTemplateforVPNNetwork(),
"aci_cloud_aws_provider": dataSourceAciCloudAWSProvider(),
"aci_autonomous_system_profile": dataSourceAciAutonomousSystemProfile(),
"aci_cloud_cidr_pool": dataSourceAciCloudCIDRPool(),
Expand Down
Loading

0 comments on commit b1ca47f

Please sign in to comment.