You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cloudstack.apache.org by "bddvlpr (via GitHub)" <gi...@apache.org> on 2026/09/17 12:38:55 UTC
[PR] Add cloudstack_network datasource [cloudstack-terraform-provider]
bddvlpr opened a new pull request, #354:
URL: https://github.com/apache/cloudstack-terraform-provider/pull/354
Adds a `cloudstack_network` datasource for retrieving attributes about an exisiting CloudStack network. Networks can be selected using the regex filters (like the other data sources) and can optionall be scoped to a project. When multiple networks match, the most recently created network is prioritized.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add cloudstack_network datasource [cloudstack-terraform-provider]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #354:
URL: https://github.com/apache/cloudstack-terraform-provider/pull/354#discussion_r4036936092
##########
cloudstack/data_source_cloudstack_network.go:
##########
@@ -0,0 +1,230 @@
+//
+// 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"
+ "time"
+
+ "github.com/apache/cloudstack-go/v2/cloudstack"
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
+)
+
+func dataSourceCloudstackNetwork() *schema.Resource {
+ return &schema.Resource{
+ Read: datasourceCloudStackNetworkRead,
+ Schema: map[string]*schema.Schema{
+ "filter": dataSourceFiltersSchema(),
+
+ // Computed values
+ "name": {
+ Type: schema.TypeString,
+ Computed: true,
+ },
+
+ "display_text": {
+ Type: schema.TypeString,
+ Computed: true,
+ },
+
+ "cidr": {
+ Type: schema.TypeString,
+ Computed: true,
+ },
+
+ "gateway": {
+ Type: schema.TypeString,
+ Computed: true,
+ },
+
+ "network_domain": {
+ Type: schema.TypeString,
+ Computed: true,
+ },
+
+ "network_offering_id": {
+ Type: schema.TypeString,
+ Computed: true,
+ },
+
+ "network_offering_name": {
+ Type: schema.TypeString,
+ Computed: true,
+ },
+
+ "project": {
+ Type: schema.TypeString,
+ Optional: true,
+ Computed: true,
+ },
+
+ "project_id": {
+ Type: schema.TypeString,
+ Computed: true,
+ },
+
+ "vpc_id": {
+ Type: schema.TypeString,
+ Computed: true,
+ },
+
+ "acl_id": {
+ Type: schema.TypeString,
+ Computed: true,
+ },
+
+ "zone_id": {
+ Type: schema.TypeString,
+ Computed: true,
+ },
+
+ "zone_name": {
+ Type: schema.TypeString,
+ Computed: true,
+ },
+
+ "state": {
+ Type: schema.TypeString,
+ Computed: true,
+ },
+
+ "type": {
+ Type: schema.TypeString,
+ Computed: true,
+ },
+
+ "traffic_type": {
+ Type: schema.TypeString,
+ Computed: true,
+ },
+
+ "tags": tagsSchema(),
+ },
+ }
+}
+
+func datasourceCloudStackNetworkRead(d *schema.ResourceData, meta interface{}) error {
+ cs := meta.(*cloudstack.CloudStackClient)
+ p := cs.Network.NewListNetworksParams()
+
+ if err := setProjectid(p, cs, d); err != nil {
+ return err
+ }
+
+ csNetworks, err := cs.Network.ListNetworks(p)
+ if err != nil {
+ return fmt.Errorf("Failed to list networks: %s", err)
+ }
+
+ filters := d.Get("filter")
+ var networks []*cloudstack.Network
+
+ for _, n := range csNetworks.Networks {
+ match, err := applyNetworkFilters(n, filters.(*schema.Set))
+ if err != nil {
+ return err
+ }
+ if match {
+ networks = append(networks, n)
+ }
+ }
+
+ if len(networks) == 0 {
+ return fmt.Errorf("No network is matching with the specified regex")
+ }
+
+ network, err := latestNetwork(networks)
+ if err != nil {
+ return err
+ }
+ log.Printf("[DEBUG] Selected network: %s\n", network.Name)
+
+ return networkDescriptionAttributes(d, network)
+}
+
+func networkDescriptionAttributes(d *schema.ResourceData, network *cloudstack.Network) error {
+ d.SetId(network.Id)
+ d.Set("name", network.Name)
+ d.Set("display_text", network.Displaytext)
+ d.Set("cidr", network.Cidr)
+ d.Set("gateway", network.Gateway)
+ d.Set("network_domain", network.Networkdomain)
+ d.Set("network_offering_id", network.Networkofferingid)
+ d.Set("network_offering_name", network.Networkofferingname)
+ d.Set("project", network.Project)
Review Comment:
`project` accepts either a name or an ID, but this unconditionally stores the API's project name. When the configuration uses a project ID, the data source state no longer preserves the selector form, which can cause a diff and changes the value used on subsequent reads. Preserve the configured representation with `setValueOrID`, as the network resource and other data sources do.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org