-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathdata_source_sysdig_secure_zone.go
More file actions
174 lines (163 loc) · 4.79 KB
/
data_source_sysdig_secure_zone.go
File metadata and controls
174 lines (163 loc) · 4.79 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package sysdig
import (
"context"
"fmt"
"strconv"
"time"
v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceSysdigSecureZone() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceSysdigSecureZoneRead,
Schema: map[string]*schema.Schema{
SchemaDescriptionKey: {
Type: schema.TypeString,
Computed: true,
},
SchemaIsSystemKey: {
Type: schema.TypeBool,
Computed: true,
},
SchemaAuthorKey: {
Type: schema.TypeString,
Computed: true,
},
SchemaLastModifiedBy: {
Type: schema.TypeString,
Computed: true,
},
SchemaLastUpdated: {
Type: schema.TypeString,
Computed: true,
},
SchemaScopeKey: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
SchemaIDKey: {
Type: schema.TypeInt,
Computed: true,
},
SchemaTargetTypeKey: {
Type: schema.TypeString,
Computed: true,
},
// Not marked Deprecated: rules with v2-compatible syntax are fully supported.
// Only v1 syntax (labels, labelValues, agentTags) is deprecated, but since
// this is a Computed field, SDK v2 has no mechanism for conditional deprecation.
// The resource-side ValidateDiagFunc handles the v1-only warning.
SchemaRulesKey: {
Type: schema.TypeString,
Computed: true,
},
SchemaExpressionKey: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
SchemaFieldKey: {Type: schema.TypeString, Computed: true},
SchemaOperatorKey: {Type: schema.TypeString, Computed: true},
SchemaValueKey: {Type: schema.TypeString, Computed: true},
SchemaValuesKey: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
},
},
},
"id": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"id", "name"},
Description: "The ID of the zone to retrieve.",
},
"name": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"id", "name"},
Description: "The name of the zone to retrieve.",
},
},
}
}
func dataSourceSysdigSecureZoneRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
clientV2, err := getZoneV2Client(m.(SysdigClients))
if err != nil {
return diag.FromErr(err)
}
var zoneV2 *v2.ZoneV2
zoneIDRaw, hasZoneID := d.GetOk("id")
if hasZoneID {
zoneID, err := strconv.Atoi(zoneIDRaw.(string))
if err != nil {
return diag.FromErr(fmt.Errorf("invalid zone id: %s", err))
}
zoneV2, err = clientV2.GetZoneV2(ctx, zoneID)
if err != nil {
return diag.FromErr(fmt.Errorf("error fetching zone v2 by ID: %s", err))
}
} else if nameRaw, hasName := d.GetOk("name"); hasName {
name := nameRaw.(string)
zones, err := clientV2.GetZonesV2(ctx, name)
if err != nil {
return diag.FromErr(fmt.Errorf("error fetching zones: %s", err))
}
for _, z := range zones {
if z.Name == name {
zoneV2 = &z
break
}
}
if zoneV2 == nil {
return diag.FromErr(fmt.Errorf("zone with name '%s' not found", name))
}
zoneV2, err = clientV2.GetZoneV2(ctx, zoneV2.ID)
if err != nil {
return diag.FromErr(fmt.Errorf("error fetching zone by name: %s", err))
}
} else {
return diag.FromErr(fmt.Errorf("either id or name must be specified"))
}
d.SetId(fmt.Sprintf("%d", zoneV2.ID))
_ = d.Set(SchemaNameKey, zoneV2.Name)
_ = d.Set(SchemaDescriptionKey, zoneV2.Description)
_ = d.Set(SchemaIsSystemKey, zoneV2.IsSystem)
_ = d.Set(SchemaAuthorKey, zoneV2.Author)
_ = d.Set(SchemaLastModifiedBy, zoneV2.LastModifiedBy)
_ = d.Set(SchemaLastUpdated, time.UnixMilli(zoneV2.LastUpdated).Format(time.RFC3339))
if err := d.Set(SchemaScopeKey, getZoneScopes(zoneV2)); err != nil {
return diag.FromErr(fmt.Errorf("error setting scope: %s", err))
}
return nil
}
func getZoneScopes(zoneV2 *v2.ZoneV2) []any {
// Build expression lookup by filter ID from the v2 response.
out := make([]any, 0)
if zoneV2 != nil {
for _, s := range zoneV2.Scopes {
for _, f := range s.Filters {
if f.ID != 0 && (len(f.Expressions) > 0 || f.Rules != "") {
var exprs []any
for _, e := range f.Expressions {
exprs = append(exprs, flattenExpressionV2(e))
}
m := map[string]any{
SchemaIDKey: f.ID,
SchemaTargetTypeKey: f.ResourceType,
SchemaRulesKey: f.Rules,
}
m[SchemaExpressionKey] = exprs
out = append(out, m)
}
}
}
}
return out
}