Skip to content

Commit 8dd4d63

Browse files
authored
Merge pull request #568 from vmware/vra_network
Add support for filters at vra_network datasource
2 parents 61b34cb + d119001 commit 8dd4d63

File tree

4 files changed

+155
-88
lines changed

4 files changed

+155
-88
lines changed

Diff for: go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
99
github.com/hashicorp/go-version v1.7.0
1010
github.com/hashicorp/terraform-plugin-sdk/v2 v2.35.0
11-
github.com/vmware/vra-sdk-go v0.6.1
11+
github.com/vmware/vra-sdk-go v0.6.2
1212
)
1313

1414
require (

Diff for: go.sum

+2-4
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,6 @@ github.com/hashicorp/terraform-registry-address v0.2.3 h1:2TAiKJ1A3MAkZlH1YI/aTV
176176
github.com/hashicorp/terraform-registry-address v0.2.3/go.mod h1:lFHA76T8jfQteVfT7caREqguFrW3c4MFSPhZB7HHgUM=
177177
github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S52uzrw4x0jKQ=
178178
github.com/hashicorp/terraform-svchost v0.1.1/go.mod h1:mNsjQfZyf/Jhz35v6/0LWcv26+X7JPS+buii2c9/ctc=
179-
github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE=
180-
github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ=
181179
github.com/hashicorp/yamux v0.1.2 h1:XtB8kyFOyHXYVFnwT5C3+Bdo8gArse7j2AQ0DA0Uey8=
182180
github.com/hashicorp/yamux v0.1.2/go.mod h1:C+zze2n6e/7wshOZep2A70/aQU6QBRWJO/G6FT1wIns=
183181
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
@@ -282,8 +280,8 @@ github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IU
282280
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
283281
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
284282
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
285-
github.com/vmware/vra-sdk-go v0.6.1 h1:2DscXG18be7MknOX1zIZFekB/NQV3eJyH1dEkX1E0+4=
286-
github.com/vmware/vra-sdk-go v0.6.1/go.mod h1:gUj5Z/+qvXEDk9+0NvW0ynDrMkTk3P5Bdt2JfN+AAC8=
283+
github.com/vmware/vra-sdk-go v0.6.2 h1:fJ03oxQj0q0wfDnVqs8vmo84L+91iZnI4cu8XXcR0b4=
284+
github.com/vmware/vra-sdk-go v0.6.2/go.mod h1:gUj5Z/+qvXEDk9+0NvW0ynDrMkTk3P5Bdt2JfN+AAC8=
287285
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
288286
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
289287
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=

Diff for: vra/data_source_network.go

+128-75
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
package vra
66

77
import (
8+
"errors"
89
"fmt"
9-
"strings"
1010

1111
"github.com/vmware/vra-sdk-go/pkg/client/network"
1212
"github.com/vmware/vra-sdk-go/pkg/models"
@@ -19,69 +19,90 @@ func dataSourceNetwork() *schema.Resource {
1919
Read: dataSourceNetworkRead,
2020
Schema: map[string]*schema.Schema{
2121
"id": {
22-
Type: schema.TypeString,
23-
Optional: true,
24-
Computed: true,
22+
Type: schema.TypeString,
23+
ConflictsWith: []string{"name", "filter"},
24+
Optional: true,
25+
Computed: true,
26+
Description: "The id of the network instance",
2527
},
2628
"name": {
27-
Type: schema.TypeString,
28-
Optional: true,
29-
Computed: true,
30-
DiffSuppressFunc: func(_, old, new string, _ *schema.ResourceData) bool {
31-
return !strings.HasPrefix(new, old)
32-
},
29+
Type: schema.TypeString,
30+
ConflictsWith: []string{"id", "filter"},
31+
Optional: true,
32+
Computed: true,
33+
Description: "The human-friendly name of the network instance",
3334
},
34-
"cidr": {
35-
Type: schema.TypeString,
36-
Computed: true,
35+
"filter": {
36+
Type: schema.TypeString,
37+
ConflictsWith: []string{"id", "name"},
38+
Optional: true,
39+
Description: "The search criteria to narrow down the network instance",
3740
},
38-
"description": {
39-
Type: schema.TypeString,
40-
Computed: true,
41+
"cidr": {
42+
Type: schema.TypeString,
43+
Computed: true,
44+
Description: "IPv4 address range of the network in CIDR format",
4145
},
42-
"deployment_id": {
43-
Type: schema.TypeString,
44-
Computed: true,
45-
},
46-
"project_id": {
47-
Type: schema.TypeString,
46+
"cloud_account_ids": {
47+
Type: schema.TypeSet,
4848
Computed: true,
49+
Elem: &schema.Schema{
50+
Type: schema.TypeString,
51+
},
52+
Description: "Set of ids of the cloud accounts this resource belongs to",
4953
},
5054
"custom_properties": {
51-
Type: schema.TypeMap,
52-
Computed: true,
55+
Type: schema.TypeMap,
56+
Computed: true,
57+
Description: "Additional properties that may be used to extend the base resource",
5358
},
54-
"constraints": constraintsSchema(),
55-
"tags": tagsSchema(),
56-
"outbound_access": {
57-
Type: schema.TypeBool,
58-
Computed: true,
59+
"deployment_id": {
60+
Type: schema.TypeString,
61+
Computed: true,
62+
Description: "Deployment id that is associated with this resource",
5963
},
60-
"external_zone_id": {
61-
Type: schema.TypeString,
62-
Computed: true,
64+
"description": {
65+
Type: schema.TypeString,
66+
Computed: true,
67+
Description: "A human-friendly description",
6368
},
6469
"external_id": {
65-
Type: schema.TypeString,
66-
Computed: true,
70+
Type: schema.TypeString,
71+
Computed: true,
72+
Description: "External entity Id on the provider side",
6773
},
68-
"self_link": {
69-
Type: schema.TypeString,
70-
Computed: true,
74+
"external_region_id": {
75+
Type: schema.TypeString,
76+
Computed: true,
77+
Description: "The external regionId of the resource",
7178
},
72-
"updated_at": {
73-
Type: schema.TypeString,
74-
Computed: true,
79+
"external_zone_id": {
80+
Type: schema.TypeString,
81+
Computed: true,
82+
Description: "The external zoneId of the resource",
83+
},
84+
"links": linksSchema(),
85+
"organization_id": {
86+
Type: schema.TypeString,
87+
Computed: true,
88+
Description: "The id of the organization this entity belongs to",
7589
},
7690
"owner": {
77-
Type: schema.TypeString,
78-
Computed: true,
91+
Type: schema.TypeString,
92+
Computed: true,
93+
Description: "Email of the user or display name of the group that owns the entity",
7994
},
80-
"organization_id": {
81-
Type: schema.TypeString,
82-
Computed: true,
95+
"project_id": {
96+
Type: schema.TypeString,
97+
Computed: true,
98+
Description: "The id of the project this resource belongs to",
99+
},
100+
"tags": tagsSchema(),
101+
"updated_at": {
102+
Type: schema.TypeString,
103+
Computed: true,
104+
Description: "Date when the entity was last updated. The date is ISO 8601 and UTC",
83105
},
84-
"links": linksSchema(),
85106
},
86107
}
87108
}
@@ -91,41 +112,73 @@ func dataSourceNetworkRead(d *schema.ResourceData, meta interface{}) error {
91112

92113
id, idOk := d.GetOk("id")
93114
name, nameOk := d.GetOk("name")
115+
filter, filterOK := d.GetOk("filter")
94116

95-
if !idOk && !nameOk {
96-
return fmt.Errorf("One of id or name must be assigned")
117+
if !idOk && !nameOk && !filterOK {
118+
return errors.New("one of id, name or filter must be assigned")
97119
}
98120

99-
getResp, err := apiClient.Network.GetNetworks(network.NewGetNetworksParams())
100-
if err != nil {
101-
return err
102-
}
121+
var net *models.Network
122+
if idOk {
123+
getResp, err := apiClient.Network.GetNetwork(network.NewGetNetworkParams().WithID(id.(string)))
124+
if err != nil {
125+
switch err.(type) {
126+
case *network.GetNetworkNotFound:
127+
return fmt.Errorf("network with id '%s' not found", id)
128+
default:
129+
// nop
130+
}
131+
return err
132+
}
103133

104-
setFields := func(network *models.Network) {
105-
d.SetId(*network.ID)
106-
d.Set("cidr", network.Cidr)
107-
d.Set("custom_properties", network.CustomProperties)
108-
d.Set("description", network.Description)
109-
d.Set("deployment_id", network.DeploymentID)
110-
d.Set("external_id", network.ExternalID)
111-
d.Set("external_zone_id", network.ExternalZoneID)
112-
d.Set("name", network.Name)
113-
d.Set("organization_id", network.OrgID)
114-
d.Set("owner", network.Owner)
115-
d.Set("project_id", network.ProjectID)
116-
d.Set("tags", network.Tags)
117-
d.Set("updated_at", network.UpdatedAt)
118-
}
119-
for _, network := range getResp.Payload.Content {
120-
if idOk && network.ID == id {
121-
setFields(network)
122-
return nil
134+
net = getResp.GetPayload()
135+
} else {
136+
var filterParam string
137+
if nameOk {
138+
filterParam = fmt.Sprintf("name eq '%s'", name.(string))
139+
} else {
140+
filterParam = filter.(string)
123141
}
124-
if nameOk && network.Name == name {
125-
setFields(network)
126-
return nil
142+
getResp, err := apiClient.Network.GetNetworks(network.NewGetNetworksParams().WithDollarFilter(&filterParam))
143+
if err != nil {
144+
return err
127145
}
146+
networks := getResp.GetPayload()
147+
if len(networks.Content) > 1 {
148+
if nameOk {
149+
return fmt.Errorf("there are more than one network with name '%s'", name)
150+
}
151+
return errors.New("must filter to one network")
152+
}
153+
if len(networks.Content) == 0 {
154+
if nameOk {
155+
return fmt.Errorf("network with name '%s' not found", name)
156+
}
157+
return fmt.Errorf("filter doesn't match to any network")
158+
}
159+
net = networks.Content[0]
160+
}
161+
162+
d.SetId(*net.ID)
163+
d.Set("cidr", net.Cidr)
164+
d.Set("cloud_account_ids", net.CloudAccountIds)
165+
d.Set("created_at", net.CreatedAt)
166+
d.Set("custom_properties", net.CustomProperties)
167+
d.Set("deployment_id", net.DeploymentID)
168+
d.Set("description", net.Description)
169+
d.Set("external_id", net.ExternalID)
170+
d.Set("external_region_id", net.ExternalRegionID)
171+
d.Set("external_zone_id", net.ExternalZoneID)
172+
d.Set("name", net.Name)
173+
d.Set("organization_id", net.OrgID)
174+
d.Set("owner", net.Owner)
175+
d.Set("project_id", net.ProjectID)
176+
d.Set("tags", net.Tags)
177+
d.Set("updated_at", net.UpdatedAt)
178+
179+
if err := d.Set("links", flattenLinks(net.Links)); err != nil {
180+
return fmt.Errorf("error setting network links - error: %#v", err)
128181
}
129182

130-
return fmt.Errorf("network %s not found", name)
183+
return nil
131184
}

Diff for: website/docs/d/vra_network.html.markdown

+24-8
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,43 @@ description: |-
99

1010
This is an example of how to read a network data source.
1111

12+
**Network data source by id:**
13+
1214
```hcl
15+
data "vra_network" "test-network" {
16+
id = var.network_id
17+
}
18+
```
1319

20+
**Network data source by name:**
21+
22+
```hcl
1423
data "vra_network" "test-network" {
1524
name = var.network_name
1625
}
26+
```
27+
28+
**Network data source by filter:**
1729

30+
```hcl
31+
data "vra_network" "test-network" {
32+
filter = "name eq '${var.network_name}'"
33+
}
1834
```
1935

2036
## Argument Reference
2137

22-
* `id` - (Optional) The id of the image profile instance.
38+
* `id` - (Optional) The id of the network instance. Only one of 'id', 'name' or 'filter' must be specified.
39+
40+
* `name` - (Optional) The human-friendly name of the network instance. Only one of 'id', 'name' or 'filter' must be specified.
2341

24-
* `name` - (Optional) A human-friendly name used as an identifier in APIs that support this option.
42+
* `filter` - (Optional) The search criteria to narrow down the network instance. Only one of 'id', 'name' or 'filter' must be specified.
2543

2644
## Attribute Reference
2745

2846
* `cidr` - IPv4 address range of the network in CIDR format.
2947

30-
* `constraints` - List of storage, network and extensibility constraints to be applied when provisioning through this project.
48+
* `cloud_account_ids` - Set of ids of the cloud accounts this resource belongs to.
3149

3250
* `custom_properties` - Additional properties that may be used to extend the base resource.
3351

@@ -37,20 +55,18 @@ data "vra_network" "test-network" {
3755

3856
* `external_id` - External entity Id on the provider side.
3957

58+
* `external_region_id` - The external regionId of the resource.
59+
4060
* `external_zone_id` - The external zoneId of the resource.
4161

4262
* `links` - HATEOAS of the entity
4363

4464
* `organization_id` - The id of the organization this entity belongs to.
4565

46-
* `outbound_access` - Flag to indicate if the network needs to have outbound access or not. Default is true. This field will be ignored if there is proper input for networkType customProperty
47-
48-
* `owner` - Email of the user that owns the entity.
66+
* `owner` - Email of the user or display name of the group that owns the entity.
4967

5068
* `project_id` - The id of the project this resource belongs to.
5169

52-
* `self_link` - Self link of this request.
53-
5470
* `tags` - A set of tag keys and optional values that were set on this resource.
5571
example: `[ { "key" : "ownedBy", "value": "Rainpole" } ]`
5672

0 commit comments

Comments
 (0)