/
datasource_vcd_nsxv_application_finder.go
127 lines (113 loc) · 3.39 KB
/
datasource_vcd_nsxv_application_finder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package vcd
import (
"context"
"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/vmware/go-vcloud-director/v2/govcd"
"github.com/vmware/go-vcloud-director/v2/types/v56"
)
func datasourceVcdNsxvApplicationFinder() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceVcdNsxvApplicationFinderRead,
Schema: map[string]*schema.Schema{
"vdc_id": {
Type: schema.TypeString,
Required: true,
Description: "The ID of VDC",
},
"search_expression": {
Type: schema.TypeString,
Required: true,
Description: "Regular expression used to search applications or groups",
},
// Note: the search is case-insensitive by default, to mimic the behavior of the UI
"case_sensitive": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Convert the search to case sensitive",
},
"type": {
Type: schema.TypeString,
Required: true,
Description: "Type of object. One of 'application', 'application_group'",
ValidateFunc: validation.StringInSlice([]string{"application", "application_group"}, false),
},
"objects": {
Type: schema.TypeSet,
Computed: true,
Description: "Objects found with the combination of 'search_expression' + 'type'",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the object",
},
"value": {
Type: schema.TypeString,
Computed: true,
Description: "Identifier of the object",
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: "Type of the object",
},
},
},
},
},
}
}
func datasourceVcdNsxvApplicationFinderRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vcdClient := meta.(*VCDClient)
vdcId := d.Get("vdc_id").(string)
dfw := govcd.NewNsxvDistributedFirewall(&vcdClient.Client, vdcId)
wantedType := d.Get("type").(string)
rawRegexp := d.Get("search_expression").(string)
rawCaseSensitive := d.Get("case_sensitive")
caseSensitive := false
if rawCaseSensitive != nil {
caseSensitive = rawCaseSensitive.(bool)
}
if !caseSensitive {
rawRegexp = `(?i)` + rawRegexp
}
var result []map[string]string
var applications []types.Application
var applicationGroups []types.ApplicationGroup
var err error
switch wantedType {
case "application":
applications, err = dfw.GetServicesByRegex(rawRegexp)
case "application_group":
applicationGroups, err = dfw.GetServiceGroupsByRegex(rawRegexp)
}
if err != nil {
return diag.Errorf("error retrieving %s list: %s - %s", wantedType, govcd.ErrorEntityNotFound, err)
}
for _, application := range applications {
item := map[string]string{
"name": application.Name,
"type": application.Type.TypeName,
"value": application.ObjectID,
}
result = append(result, item)
}
for _, ag := range applicationGroups {
item := map[string]string{
"name": ag.Name,
"type": ag.Type.TypeName,
"value": ag.ObjectID,
}
result = append(result, item)
}
err = d.Set("objects", result)
if err != nil {
return diag.FromErr(err)
}
d.SetId(vdcId)
return nil
}