Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
140 changes: 140 additions & 0 deletions cloudstack/data_source_cloudstack_physicalnetwork.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//

package cloudstack

import (
"encoding/json"
"fmt"
"log"
"regexp"
"strings"

"github.com/apache/cloudstack-go/v2/cloudstack"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceCloudStackPhysicalNetwork() *schema.Resource {
return &schema.Resource{
Read: dataSourceCloudStackPhysicalNetworkRead,
Schema: map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),

//Computed values
"name": {
Type: schema.TypeString,
Computed: true,
},
"zone": {
Type: schema.TypeString,
Computed: true,
},
"broadcast_domain_range": {
Type: schema.TypeString,
Computed: true,
},
"isolation_methods": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"network_speed": {
Type: schema.TypeString,
Computed: true,
},
"vlan": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceCloudStackPhysicalNetworkRead(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
p := cs.Network.NewListPhysicalNetworksParams()
physicalNetworks, err := cs.Network.ListPhysicalNetworks(p)

if err != nil {
return fmt.Errorf("Failed to list physical networks: %s", err)
}
filters := d.Get("filter")
var physicalNetwork *cloudstack.PhysicalNetwork

for _, pn := range physicalNetworks.PhysicalNetworks {
match, err := applyPhysicalNetworkFilters(pn, filters.(*schema.Set))
if err != nil {
return err
}
if match {
physicalNetwork = pn
}
}

if physicalNetwork == nil {
return fmt.Errorf("No physical network is matching with the specified regex")
}
log.Printf("[DEBUG] Selected physical network: %s\n", physicalNetwork.Name)

return physicalNetworkDescriptionAttributes(d, physicalNetwork)
}

func physicalNetworkDescriptionAttributes(d *schema.ResourceData, physicalNetwork *cloudstack.PhysicalNetwork) error {
d.SetId(physicalNetwork.Id)
d.Set("name", physicalNetwork.Name)
d.Set("broadcast_domain_range", physicalNetwork.Broadcastdomainrange)
d.Set("network_speed", physicalNetwork.Networkspeed)
d.Set("vlan", physicalNetwork.Vlan)

// Set isolation methods
if physicalNetwork.Isolationmethods != "" {
methods := strings.Split(physicalNetwork.Isolationmethods, ",")
d.Set("isolation_methods", methods)
}

// Set the zone
d.Set("zone", physicalNetwork.Zonename)

// Physical networks don't support tags in CloudStack API

return nil
}

func applyPhysicalNetworkFilters(physicalNetwork *cloudstack.PhysicalNetwork, filters *schema.Set) (bool, error) {
var physicalNetworkJSON map[string]interface{}
k, _ := json.Marshal(physicalNetwork)
err := json.Unmarshal(k, &physicalNetworkJSON)
if err != nil {
return false, err
}

for _, f := range filters.List() {
m := f.(map[string]interface{})
r, err := regexp.Compile(m["value"].(string))
if err != nil {
return false, fmt.Errorf("Invalid regex: %s", err)
}
updatedName := strings.ReplaceAll(m["name"].(string), "_", "")
physicalNetworkField := physicalNetworkJSON[updatedName].(string)
if !r.MatchString(physicalNetworkField) {
return false, nil
}
}
return true, nil
}
67 changes: 67 additions & 0 deletions cloudstack/data_source_cloudstack_physicalnetwork_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//

package cloudstack

import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccDataSourceCloudStackPhysicalNetwork_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceCloudStackPhysicalNetwork_basic,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.cloudstack_physicalnetwork.foo", "name", "terraform-physical-network"),
resource.TestCheckResourceAttr(
"data.cloudstack_physicalnetwork.foo", "broadcast_domain_range", "ZONE"),
),
},
},
})
}

const testAccDataSourceCloudStackPhysicalNetwork_basic = `
resource "cloudstack_zone" "foo" {
name = "terraform-zone-ds"
dns1 = "8.8.8.8"
internal_dns1 = "8.8.4.4"
network_type = "Advanced"
}

resource "cloudstack_physicalnetwork" "foo" {
name = "terraform-physical-network"
zone = cloudstack_zone.foo.name
broadcast_domain_range = "ZONE"
isolation_methods = ["VLAN"]
}

data "cloudstack_physicalnetwork" "foo" {
filter {
name = "name"
value = "terraform-physical-network"
}
depends_on = [cloudstack_physicalnetwork.foo]
}`
82 changes: 43 additions & 39 deletions cloudstack/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,54 +90,58 @@ func Provider() *schema.Provider {
"cloudstack_user": dataSourceCloudstackUser(),
"cloudstack_vpn_connection": dataSourceCloudstackVPNConnection(),
"cloudstack_pod": dataSourceCloudstackPod(),
"cloudstack_physicalnetwork": dataSourceCloudStackPhysicalNetwork(),
},

ResourcesMap: map[string]*schema.Resource{
"cloudstack_affinity_group": resourceCloudStackAffinityGroup(),
"cloudstack_attach_volume": resourceCloudStackAttachVolume(),
"cloudstack_autoscale_vm_profile": resourceCloudStackAutoScaleVMProfile(),
"cloudstack_configuration": resourceCloudStackConfiguration(),
"cloudstack_disk": resourceCloudStackDisk(),
"cloudstack_egress_firewall": resourceCloudStackEgressFirewall(),
"cloudstack_firewall": resourceCloudStackFirewall(),
"cloudstack_host": resourceCloudStackHost(),
"cloudstack_instance": resourceCloudStackInstance(),
"cloudstack_ipaddress": resourceCloudStackIPAddress(),
"cloudstack_kubernetes_cluster": resourceCloudStackKubernetesCluster(),
"cloudstack_kubernetes_version": resourceCloudStackKubernetesVersion(),
"cloudstack_loadbalancer_rule": resourceCloudStackLoadBalancerRule(),
"cloudstack_network": resourceCloudStackNetwork(),
"cloudstack_network_acl": resourceCloudStackNetworkACL(),
"cloudstack_network_acl_rule": resourceCloudStackNetworkACLRule(),
"cloudstack_nic": resourceCloudStackNIC(),
"cloudstack_port_forward": resourceCloudStackPortForward(),
"cloudstack_private_gateway": resourceCloudStackPrivateGateway(),
"cloudstack_secondary_ipaddress": resourceCloudStackSecondaryIPAddress(),
"cloudstack_security_group": resourceCloudStackSecurityGroup(),
"cloudstack_security_group_rule": resourceCloudStackSecurityGroupRule(),
"cloudstack_ssh_keypair": resourceCloudStackSSHKeyPair(),
"cloudstack_static_nat": resourceCloudStackStaticNAT(),
"cloudstack_static_route": resourceCloudStackStaticRoute(),
"cloudstack_template": resourceCloudStackTemplate(),
"cloudstack_vpc": resourceCloudStackVPC(),
"cloudstack_vpn_connection": resourceCloudStackVPNConnection(),
"cloudstack_vpn_customer_gateway": resourceCloudStackVPNCustomerGateway(),
"cloudstack_vpn_gateway": resourceCloudStackVPNGateway(),
"cloudstack_network_offering": resourceCloudStackNetworkOffering(),
"cloudstack_disk_offering": resourceCloudStackDiskOffering(),
"cloudstack_volume": resourceCloudStackVolume(),
"cloudstack_zone": resourceCloudStackZone(),
"cloudstack_service_offering": resourceCloudStackServiceOffering(),
"cloudstack_account": resourceCloudStackAccount(),
"cloudstack_user": resourceCloudStackUser(),
"cloudstack_domain": resourceCloudStackDomain(),
"cloudstack_affinity_group": resourceCloudStackAffinityGroup(),
"cloudstack_attach_volume": resourceCloudStackAttachVolume(),
"cloudstack_autoscale_vm_profile": resourceCloudStackAutoScaleVMProfile(),
"cloudstack_configuration": resourceCloudStackConfiguration(),
"cloudstack_disk": resourceCloudStackDisk(),
"cloudstack_egress_firewall": resourceCloudStackEgressFirewall(),
"cloudstack_firewall": resourceCloudStackFirewall(),
"cloudstack_host": resourceCloudStackHost(),
"cloudstack_instance": resourceCloudStackInstance(),
"cloudstack_ipaddress": resourceCloudStackIPAddress(),
"cloudstack_kubernetes_cluster": resourceCloudStackKubernetesCluster(),
"cloudstack_kubernetes_version": resourceCloudStackKubernetesVersion(),
"cloudstack_loadbalancer_rule": resourceCloudStackLoadBalancerRule(),
"cloudstack_network": resourceCloudStackNetwork(),
"cloudstack_network_acl": resourceCloudStackNetworkACL(),
"cloudstack_network_acl_rule": resourceCloudStackNetworkACLRule(),
"cloudstack_nic": resourceCloudStackNIC(),
"cloudstack_port_forward": resourceCloudStackPortForward(),
"cloudstack_private_gateway": resourceCloudStackPrivateGateway(),
"cloudstack_secondary_ipaddress": resourceCloudStackSecondaryIPAddress(),
"cloudstack_security_group": resourceCloudStackSecurityGroup(),
"cloudstack_security_group_rule": resourceCloudStackSecurityGroupRule(),
"cloudstack_ssh_keypair": resourceCloudStackSSHKeyPair(),
"cloudstack_static_nat": resourceCloudStackStaticNAT(),
"cloudstack_static_route": resourceCloudStackStaticRoute(),
"cloudstack_template": resourceCloudStackTemplate(),
"cloudstack_vpc": resourceCloudStackVPC(),
"cloudstack_vpn_connection": resourceCloudStackVPNConnection(),
"cloudstack_vpn_customer_gateway": resourceCloudStackVPNCustomerGateway(),
"cloudstack_vpn_gateway": resourceCloudStackVPNGateway(),
"cloudstack_network_offering": resourceCloudStackNetworkOffering(),
"cloudstack_disk_offering": resourceCloudStackDiskOffering(),
"cloudstack_volume": resourceCloudStackVolume(),
"cloudstack_zone": resourceCloudStackZone(),
"cloudstack_service_offering": resourceCloudStackServiceOffering(),
"cloudstack_account": resourceCloudStackAccount(),
"cloudstack_user": resourceCloudStackUser(),
"cloudstack_domain": resourceCloudStackDomain(),
"cloudstack_physicalnetwork": resourceCloudStackPhysicalNetwork(),
"cloudstack_traffic_type": resourceCloudStackTrafficType(),
"cloudstack_network_service_provider": resourceCloudStackNetworkServiceProvider(),
},

ConfigureFunc: providerConfigure,
}
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
func providerConfigure(d *schema.ResourceData) (any, error) {
apiURL, apiURLOK := d.GetOk("api_url")
apiKey, apiKeyOK := d.GetOk("api_key")
secretKey, secretKeyOK := d.GetOk("secret_key")
Expand Down
Loading