Repository navigation
Expand file tree
/
Copy pathapplication_group_resource_model.go
More file actions
240 lines (220 loc) · 10.2 KB
/
Copy pathapplication_group_resource_model.go
File metadata and controls
240 lines (220 loc) · 10.2 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright © 2024. Citrix Systems, Inc.
package application
import (
"context"
"regexp"
"strings"
citrixorchestration "github.com/citrix/citrix-daas-rest-go/citrixorchestration"
citrixdaasclient "github.com/citrix/citrix-daas-rest-go/client"
"github.com/citrix/terraform-provider-citrix/internal/util"
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/setdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/setplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
)
// ApplicationGroupResource maps the resource schema data.
type ApplicationGroupResourceModel struct {
Id types.String `tfsdk:"id"`
Enabled types.Bool `tfsdk:"enabled"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
RestrictToTag types.String `tfsdk:"restrict_to_tag"`
IncludedUsers types.Set `tfsdk:"included_users"` // Set[string]
DeliveryGroups types.Set `tfsdk:"delivery_groups"` // Set[string]
Scopes types.Set `tfsdk:"scopes"` // Set[string]
BuiltInScopes types.Set `tfsdk:"built_in_scopes"` //Set[String]
InheritedScopes types.Set `tfsdk:"inherited_scopes"` //Set[String]
ApplicationGroupFolderPath types.String `tfsdk:"application_group_folder_path"`
Tenants types.Set `tfsdk:"tenants"` // Set[string]
Metadata types.List `tfsdk:"metadata"` // List[NameValueStringPairModel]
Tags types.Set `tfsdk:"tags"` // Set[string]
}
func (ApplicationGroupResourceModel) GetSchema() schema.Schema {
return schema.Schema{
Description: "CVAD --- Resource for creating and managing application group.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "GUID identifier of the application group.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"enabled": schema.BoolAttribute{
Description: "Whether the application group is enabled or not. Defaults to `true`.",
Computed: true,
Optional: true,
Default: booldefault.StaticBool(true),
},
"name": schema.StringAttribute{
Description: "Name of the application group to create.",
Required: true,
},
"description": schema.StringAttribute{
Description: "Description of the application group.",
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
},
"restrict_to_tag": schema.StringAttribute{
Description: "The tag to restrict the application group to.",
Optional: true,
},
"included_users": schema.SetAttribute{
ElementType: types.StringType,
Description: "Users who can use this application group. " +
"\n\n-> **Note** User must be in SID, SAM account name (`DOMAIN\\UserOrGroupName`) or UPN (`user@domain.com`) format",
Optional: true,
Validators: []validator.Set{
setvalidator.ValueStringsAre(
validator.String(
stringvalidator.RegexMatches(regexp.MustCompile(util.SamUpnSidRegex), "must be in SID, SAM account name (`DOMAIN\\UserOrGroupName`) or UPN (`user@domain.com`) format"),
),
),
},
},
"delivery_groups": schema.SetAttribute{
ElementType: types.StringType,
Description: "Delivery groups to associate with the application group.",
Required: true,
Validators: []validator.Set{
setvalidator.ValueStringsAre(
validator.String(
stringvalidator.RegexMatches(regexp.MustCompile(util.GuidRegex), "must be specified with ID in GUID format"),
),
),
},
},
"scopes": schema.SetAttribute{
ElementType: types.StringType,
Description: "The IDs of the scopes for the application group to be a part of.",
Optional: true,
Computed: true,
Default: setdefault.StaticValue(types.SetValueMust(types.StringType, []attr.Value{})),
Validators: []validator.Set{
setvalidator.ValueStringsAre(
validator.String(
stringvalidator.RegexMatches(regexp.MustCompile(util.GuidRegex), "must be specified with ID in GUID format"),
),
),
},
},
"built_in_scopes": schema.SetAttribute{
ElementType: types.StringType,
Description: "The IDs of the built-in scopes of the application group.",
Computed: true,
PlanModifiers: []planmodifier.Set{
setplanmodifier.UseStateForUnknown(),
},
},
"inherited_scopes": schema.SetAttribute{
ElementType: types.StringType,
Description: "The IDs of the inherited scopes of the application group.",
Computed: true,
},
"application_group_folder_path": schema.StringAttribute{
Description: "The path of the folder in which the application group is located.",
Optional: true,
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile(util.AdminFolderPathWithBackslashRegex), "Admin Folder Path must not start or end with a backslash"),
stringvalidator.RegexMatches(regexp.MustCompile(util.AdminFolderPathSpecialCharactersRegex), "Admin Folder Path must not contain any of the following special characters: / ; : # . * ? = < > | [ ] ( ) { } \" ' ` ~ "),
},
},
"tenants": schema.SetAttribute{
ElementType: types.StringType,
Description: "A set of identifiers of tenants to associate with the application group.",
Computed: true,
},
"metadata": util.GetMetadataListSchema("Application Group"),
"tags": schema.SetAttribute{
ElementType: types.StringType,
Description: "A set of identifiers of tags to associate with the application group.",
Optional: true,
Validators: []validator.Set{
setvalidator.SizeAtLeast(1),
setvalidator.ValueStringsAre(
validator.String(
stringvalidator.RegexMatches(regexp.MustCompile(util.GuidRegex), "must be specified with ID in GUID format"),
),
),
},
},
},
}
}
func (ApplicationGroupResourceModel) GetAttributesNamesToMask() map[string]bool {
return map[string]bool{
"included_users": true,
}
}
func (ApplicationGroupResourceModel) GetAttributes() map[string]schema.Attribute {
return ApplicationGroupResourceModel{}.GetSchema().Attributes
}
func (r ApplicationGroupResourceModel) RefreshPropertyValues(ctx context.Context, diagnostics *diag.Diagnostics, client *citrixdaasclient.CitrixDaasClient, applicationGroup *citrixorchestration.ApplicationGroupDetailResponseModel, dgs *citrixorchestration.ApplicationGroupDeliveryGroupResponseModelCollection, tags []string) ApplicationGroupResourceModel {
// Overwrite application with refreshed state
r.Id = types.StringValue(applicationGroup.GetId())
r.Name = types.StringValue(applicationGroup.GetName())
// Set optional values
r.Enabled = types.BoolValue(applicationGroup.GetEnabled())
r.Description = types.StringValue(applicationGroup.GetDescription())
restrictToTag := applicationGroup.GetRestrictToTag()
retrictToTagName := restrictToTag.GetName()
if retrictToTagName != "" {
r.RestrictToTag = types.StringValue(retrictToTagName)
} else {
r.RestrictToTag = types.StringNull()
}
// Set included users
includedUsers := applicationGroup.GetIncludedUsers() //only set IncludedUsers to null when it is null in the configuration
if len(includedUsers) == 0 && r.IncludedUsers.IsNull() {
r.IncludedUsers = types.SetNull(types.StringType)
} else { // If included users is not null or empty list, we need to update the list
r.IncludedUsers = util.RefreshUsersList(ctx, diagnostics, r.IncludedUsers, includedUsers)
}
resultDeliveryGroupIds := []string{}
for _, deliveryGroup := range dgs.Items {
resultDeliveryGroupIds = append(resultDeliveryGroupIds, deliveryGroup.GetId())
}
r.DeliveryGroups = util.StringArrayToStringSet(ctx, diagnostics, resultDeliveryGroupIds)
scopeIdsInPlan := util.StringSetToStringArray(ctx, diagnostics, r.Scopes)
scopeIds, builtInScopes, inheritedScopeIds, err := util.CategorizeScopes(ctx, client, diagnostics, applicationGroup.GetScopes(), citrixorchestration.SCOPEDOBJECTTYPE_DELIVERY_GROUP, resultDeliveryGroupIds, scopeIdsInPlan)
if err != nil {
return r
}
r.Scopes = util.StringArrayToStringSet(ctx, diagnostics, scopeIds)
r.BuiltInScopes = util.StringArrayToStringSet(ctx, diagnostics, builtInScopes)
r.InheritedScopes = util.StringArrayToStringSet(ctx, diagnostics, inheritedScopeIds)
adminFolder := applicationGroup.GetAdminFolder()
adminFolderPath := strings.TrimSuffix(adminFolder.GetName(), "\\")
if adminFolderPath != "" {
r.ApplicationGroupFolderPath = types.StringValue(adminFolderPath)
} else {
r.ApplicationGroupFolderPath = types.StringNull()
}
if len(applicationGroup.GetTenants()) > 0 {
var remoteTenants []string
for _, tenant := range applicationGroup.GetTenants() {
remoteTenants = append(remoteTenants, tenant.GetId())
}
r.Tenants = util.StringArrayToStringSet(ctx, diagnostics, remoteTenants)
} else {
r.Tenants = types.SetNull(types.StringType)
}
effectiveMetadata := util.GetEffectiveMetadata(util.ObjectListToTypedArray[util.NameValueStringPairModel](ctx, diagnostics, r.Metadata), applicationGroup.GetMetadata())
if len(effectiveMetadata) > 0 {
r.Metadata = util.RefreshListValueProperties[util.NameValueStringPairModel, citrixorchestration.NameValueStringPairModel](ctx, diagnostics, r.Metadata, effectiveMetadata, util.GetOrchestrationNameValueStringPairKey)
} else {
r.Metadata = util.TypedArrayToObjectList[util.NameValueStringPairModel](ctx, diagnostics, nil)
}
r.Tags = util.RefreshTagSet(ctx, diagnostics, tags)
return r
}