/
data_akamai_cloudlets_shared_policy.go
330 lines (302 loc) · 12.1 KB
/
data_akamai_cloudlets_shared_policy.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package cloudlets
import (
"context"
"encoding/json"
"errors"
"fmt"
v3 "github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/cloudlets/v3"
"github.com/akamai/terraform-provider-akamai/v6/pkg/meta"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
type sharedPolicyModel struct {
ID types.String `tfsdk:"id"`
PolicyID types.Int64 `tfsdk:"policy_id"`
Version types.Int64 `tfsdk:"version"`
VersionDescription types.String `tfsdk:"version_description"`
GroupID types.Int64 `tfsdk:"group_id"`
Name types.String `tfsdk:"name"`
CloudletType types.String `tfsdk:"cloudlet_type"`
Description types.String `tfsdk:"description"`
MatchRules types.String `tfsdk:"match_rules"`
Warnings types.String `tfsdk:"warnings"`
Activations *activationModel `tfsdk:"activations"`
}
type activationModel struct {
Production activationInfoModel `tfsdk:"production"`
Staging activationInfoModel `tfsdk:"staging"`
}
type activationInfoModel struct {
Effective *policyActivationModel `tfsdk:"effective"`
Latest *policyActivationModel `tfsdk:"latest"`
}
type policyActivationModel struct {
ActivationID types.Int64 `tfsdk:"activation_id"`
CreatedBy types.String `tfsdk:"created_by"`
CreatedDate types.String `tfsdk:"created_date"`
FinishDate types.String `tfsdk:"finish_date"`
Network types.String `tfsdk:"network"`
Operation types.String `tfsdk:"operation"`
PolicyID types.Int64 `tfsdk:"policy_id"`
PolicyVersion types.Int64 `tfsdk:"policy_version"`
Status types.String `tfsdk:"status"`
PolicyVersionDeleted types.Bool `tfsdk:"policy_version_deleted"`
}
var (
_ datasource.DataSource = &sharedPolicyDataSource{}
_ datasource.DataSourceWithConfigure = &sharedPolicyDataSource{}
)
type sharedPolicyDataSource struct {
meta meta.Meta
}
// NewSharedPolicyDataSource returns a new cloudlets shared policy data source
func NewSharedPolicyDataSource() datasource.DataSource {
return &sharedPolicyDataSource{}
}
func (d *sharedPolicyDataSource) name() string {
return "akamai_cloudlets_shared_policy"
}
// Metadata configures data source's meta information
func (d *sharedPolicyDataSource) Metadata(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = d.name()
}
// Configure configures data source at the beginning of the lifecycle
func (d *sharedPolicyDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
// ProviderData is nil when Configure is run first time as part of ValidateDataSourceConfig in framework provider
return
}
defer func() {
if r := recover(); r != nil {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected meta.Meta, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
}
}()
d.meta = meta.Must(req.ProviderData)
}
// Schema is used to define data source's terraform schema
func (d *sharedPolicyDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
policyActivationAttributes := map[string]schema.Attribute{
"activation_id": schema.Int64Attribute{
Computed: true,
Description: "Identifies the activation.",
},
"created_by": schema.StringAttribute{
Computed: true,
Description: "The username who created the activation.",
},
"created_date": schema.StringAttribute{
Computed: true,
Description: "ISO 8601 timestamp indicating when the activation was created.",
},
"finish_date": schema.StringAttribute{
Computed: true,
Description: "ISO 8601 timestamp indicating when the activation ended, either successfully or unsuccessfully. You can check details of unsuccessful attempts in 'failureDetails'.",
},
"network": schema.StringAttribute{
Computed: true,
Description: "The networks where you can activate or deactivate the policy version, either 'PRODUCTION' or 'STAGING'.",
},
"operation": schema.StringAttribute{
Computed: true,
Description: "The operations that you can perform on a policy version, either 'ACTIVATION' or 'DEACTIVATION'.",
},
"policy_id": schema.Int64Attribute{
Computed: true,
Description: "Identifies the shared policy.",
},
"policy_version": schema.Int64Attribute{
Computed: true,
Description: "The number of the policy version.",
},
"status": schema.StringAttribute{
Computed: true,
Description: "The status of the operation, either 'IN_PROGRESS', 'SUCCESS', or 'FAILED'.",
},
"policy_version_deleted": schema.BoolAttribute{
Computed: true,
Description: "Indicates if the policy version is deleted.",
},
}
activationBlock := map[string]schema.Block{
"effective": schema.SingleNestedBlock{
Description: "The status of the activation that's currently in use on this network, or null if the policy has no activations.",
Attributes: policyActivationAttributes,
},
"latest": schema.SingleNestedBlock{
Description: "The status of the latest activation or null if the policy has no activations.",
Attributes: policyActivationAttributes,
},
}
resp.Schema = schema.Schema{
Description: "Cloudlets Shared Policy",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
DeprecationMessage: "Required by the terraform plugin testing framework, always set to `akamai_cloudlets_shared_policy`.",
Description: "ID of the data source.",
},
"policy_id": schema.Int64Attribute{
Required: true,
Description: "An integer ID that is associated with a policy.",
},
"version": schema.Int64Attribute{
Optional: true,
Description: "The number of the policy version.",
},
"version_description": schema.StringAttribute{
Computed: true,
Description: "A human-readable label for the policy version.",
},
"group_id": schema.Int64Attribute{
Computed: true,
Description: "Identifies the group where to which policy is assigned.",
},
"name": schema.StringAttribute{
Computed: true,
Description: "The name of the policy.",
},
"cloudlet_type": schema.StringAttribute{
Computed: true,
Description: "The two-letter code of the Cloudlet that the shared policy is for (AP, AS, CD, ER, FR, IG).",
},
"description": schema.StringAttribute{
Computed: true,
Description: "A human-readable label for the policy.",
},
"match_rules": schema.StringAttribute{
Computed: true,
Description: "A list of Cloudlet-specific match rules for this shared policy as a JSON",
},
"warnings": schema.StringAttribute{
Computed: true,
Description: "A JSON encoded list of warnings.",
},
},
Blocks: map[string]schema.Block{
"activations": schema.SingleNestedBlock{
Description: "Information about the active policy version that's currently in use and the status of the most recent activation or deactivation operation on the policy's versions for the production and staging networks.",
Blocks: map[string]schema.Block{
"production": schema.SingleNestedBlock{
Description: "The policy version number that's currently in use on this network and the status of the most recent activation or deactivation operation for this policy's versions.",
Blocks: activationBlock,
},
"staging": schema.SingleNestedBlock{
Description: "The policy version number that's currently in use on this network and the status of the most recent activation or deactivation operation for this policy's versions.",
Blocks: activationBlock,
},
},
},
},
}
}
// Read is called when the provider must read data source values in order to update state
func (d *sharedPolicyDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
tflog.Debug(ctx, "Cloudlets Shared Policy DataSource Read")
var data sharedPolicyModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
client := ClientV3(d.meta)
policy, err := client.GetPolicy(ctx, v3.GetPolicyRequest{
PolicyID: data.PolicyID.ValueInt64(),
})
if err != nil {
if errors.Is(err, v3.ErrPolicyNotFound) {
resp.Diagnostics.AddError("Policy does not exist or is not of 'SHARED' type", err.Error())
return
}
resp.Diagnostics.AddError("Reading Cloudlets Shared Policy Failed", err.Error())
return
}
version := data.Version.ValueInt64()
if version == 0 {
policyVersions, err := client.ListPolicyVersions(ctx, v3.ListPolicyVersionsRequest{
PolicyID: data.PolicyID.ValueInt64(),
})
if err != nil {
resp.Diagnostics.AddError("Reading Cloudlets Shared Policy Failed", err.Error())
return
}
if len(policyVersions.PolicyVersions) != 0 {
version = policyVersions.PolicyVersions[0].PolicyVersion
}
}
var policyVersion *v3.PolicyVersion
if version != 0 {
policyVersion, err = client.GetPolicyVersion(ctx, v3.GetPolicyVersionRequest{
PolicyID: data.PolicyID.ValueInt64(),
PolicyVersion: version,
})
if err != nil {
resp.Diagnostics.AddError("Reading Cloudlets Shared Policy Failed", err.Error())
return
}
matchRulesWarnings, err := json.Marshal(policyVersion.MatchRulesWarnings)
if err != nil {
resp.Diagnostics.AddError("Reading Cloudlets Shared Policy Failed", err.Error())
return
}
data.Warnings = types.StringValue(string(matchRulesWarnings))
matchRules, err := json.Marshal(policyVersion.MatchRules)
if err != nil {
resp.Diagnostics.AddError("Reading Cloudlets Shared Policy Failed", err.Error())
return
}
data.MatchRules = types.StringValue(string(matchRules))
data.Version = types.Int64Value(version)
if policyVersion.Description != nil {
data.VersionDescription = types.StringValue(*policyVersion.Description)
}
}
data.setPolicyData(policy)
data.setActivations(policy.CurrentActivations)
resp.Diagnostics.Append(resp.State.Set(ctx, data)...)
}
func (m *sharedPolicyModel) setPolicyData(policy *v3.Policy) {
m.CloudletType = types.StringValue(string(policy.CloudletType))
m.Name = types.StringValue(policy.Name)
m.ID = types.StringValue("akamai_cloudlets_shared_policy")
m.GroupID = types.Int64Value(policy.GroupID)
if policy.Description != nil {
m.Description = types.StringValue(*policy.Description)
}
}
func (m *sharedPolicyModel) setActivations(activations v3.CurrentActivations) {
actMod := &activationModel{
Production: activationInfoModel{},
Staging: activationInfoModel{},
}
if activations.Production.Effective != nil {
actMod.Production.Effective = getActivationModel(activations.Production.Effective)
}
if activations.Production.Latest != nil {
actMod.Production.Latest = getActivationModel(activations.Production.Latest)
}
if activations.Staging.Effective != nil {
actMod.Staging.Effective = getActivationModel(activations.Staging.Effective)
}
if activations.Staging.Latest != nil {
actMod.Staging.Latest = getActivationModel(activations.Staging.Latest)
}
m.Activations = actMod
}
func getActivationModel(activation *v3.PolicyActivation) *policyActivationModel {
return &policyActivationModel{
ActivationID: types.Int64Value(activation.ID),
CreatedBy: types.StringValue(activation.CreatedBy),
CreatedDate: types.StringValue(activation.CreatedDate.String()),
FinishDate: types.StringValue(activation.FinishDate.String()),
Network: types.StringValue(string(activation.Network)),
Operation: types.StringValue(string(activation.Operation)),
PolicyID: types.Int64Value(activation.PolicyID),
PolicyVersion: types.Int64Value(activation.PolicyVersion),
Status: types.StringValue(string(activation.Status)),
PolicyVersionDeleted: types.BoolValue(activation.PolicyVersionDeleted),
}
}