/
data_akamai_cloudwrapper_capacities.go
214 lines (187 loc) · 6.95 KB
/
data_akamai_cloudwrapper_capacities.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
package cloudwrapper
import (
"context"
"fmt"
"strings"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v7/pkg/cloudwrapper"
"github.com/akamai/terraform-provider-akamai/v5/pkg/common/collections"
"github.com/akamai/terraform-provider-akamai/v5/pkg/meta"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
type capacitiesDataSourceModel struct {
ID types.String `tfsdk:"id"`
ContractIDs types.List `tfsdk:"contract_ids"`
Capacities []locationCapacityModel `tfsdk:"capacities"`
}
func (m capacitiesDataSourceModel) getContractIDs(ctx context.Context) ([]string, diag.Diagnostics) {
var contracts []string
diags := m.ContractIDs.ElementsAs(ctx, &contracts, false)
if diags.HasError() {
return nil, diags
}
collections.ForEachInSlice(contracts, func(s string) string {
return strings.TrimPrefix(s, "ctr_")
})
return contracts, nil
}
type locationCapacityModel struct {
LocationID types.Int64 `tfsdk:"location_id"`
LocationName types.String `tfsdk:"location_name"`
ContractID types.String `tfsdk:"contract_id"`
Type types.String `tfsdk:"type"`
ApprovedCapacity capacityModel `tfsdk:"approved"`
AssignedCapacity capacityModel `tfsdk:"assigned"`
UnassignedCapacity capacityModel `tfsdk:"unassigned"`
}
type capacityModel struct {
Value types.Int64 `tfsdk:"value"`
Unit types.String `tfsdk:"unit"`
}
func newLocationCapacityModel(capacity cloudwrapper.LocationCapacity) locationCapacityModel {
return locationCapacityModel{
LocationID: types.Int64Value(int64(capacity.LocationID)),
LocationName: types.StringValue(capacity.LocationName),
ContractID: types.StringValue(capacity.ContractID),
Type: types.StringValue(string(capacity.Type)),
ApprovedCapacity: newCapacityModel(capacity.ApprovedCapacity),
AssignedCapacity: newCapacityModel(capacity.AssignedCapacity),
UnassignedCapacity: newCapacityModel(capacity.UnassignedCapacity),
}
}
func newCapacityModel(capacity cloudwrapper.Capacity) capacityModel {
return capacityModel{
Value: types.Int64Value(int64(capacity.Value)),
Unit: types.StringValue(string(capacity.Unit)),
}
}
var (
_ datasource.DataSource = &capacitiesDataSource{}
_ datasource.DataSourceWithConfigure = &capacitiesDataSource{}
)
type capacitiesDataSource struct {
client cloudwrapper.CloudWrapper
}
// NewCapacitiesDataSource returns a new capacity data source
func NewCapacitiesDataSource() datasource.DataSource {
return &capacitiesDataSource{}
}
func (d *capacitiesDataSource) setClient(client cloudwrapper.CloudWrapper) {
d.client = client
}
// Metadata configures data source's meta information
func (d *capacitiesDataSource) Metadata(_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = d.name()
}
func (d *capacitiesDataSource) name() string {
return "akamai_cloudwrapper_capacities"
}
// Configure configures data source at the beginning of the lifecycle
func (d *capacitiesDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
if d.client != nil {
return
}
m, ok := req.ProviderData.(meta.Meta)
if !ok {
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.client = cloudwrapper.Client(m.Session())
}
// Schema is used to define data source's terraform schema
func (d *capacitiesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
capacityAttributes := map[string]attr.Type{
"value": types.Int64Type,
"unit": types.StringType,
}
resp.Schema = schema.Schema{
Description: "CloudWrapper capacities",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
DeprecationMessage: "Required by the terraform plugin testing framework, always set to `akamai_cloudwrapper_capacities`.",
Description: "ID of the data source.",
},
"contract_ids": schema.ListAttribute{
ElementType: types.StringType,
Optional: true,
Description: "List of contract IDs with Cloud Wrapper entitlement.",
},
},
Blocks: map[string]schema.Block{
"capacities": schema.ListNestedBlock{
Description: "List of all location capacities.",
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"location_id": schema.Int64Attribute{
Computed: true,
Description: "Unique identifier of the configured location.",
},
"location_name": schema.StringAttribute{
Computed: true,
Description: "Name of the location.",
},
"contract_id": schema.StringAttribute{
Computed: true,
Description: "Contract ID having Cloud Wrapper entitlement.",
},
"type": schema.StringAttribute{
Computed: true,
Description: "The type of property this capacity is related to.",
},
"approved": schema.ObjectAttribute{
Computed: true,
Description: "Capacity allocated for the location.",
AttributeTypes: capacityAttributes,
},
"assigned": schema.ObjectAttribute{
Computed: true,
Description: "Capacity already assigned to Cloud Wrapper configurations.",
AttributeTypes: capacityAttributes,
},
"unassigned": schema.ObjectAttribute{
Computed: true,
Description: "Capacity value that can be assigned to Cloud Wrapper configurations.",
AttributeTypes: capacityAttributes,
},
},
},
},
},
}
}
// Read is called when the provider must read data source values in order to update state
func (d *capacitiesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
tflog.Debug(ctx, "CloudWrapper Capacities DataSource Read")
var data capacitiesDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
contractIDs, diags := data.getContractIDs(ctx)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
}
listCapacitiesResponse, err := d.client.ListCapacities(ctx, cloudwrapper.ListCapacitiesRequest{
ContractIDs: contractIDs,
})
if err != nil {
resp.Diagnostics.AddError("Reading CloudWrapper Capacities Failed", err.Error())
return
}
for _, capacity := range listCapacitiesResponse.Capacities {
data.Capacities = append(data.Capacities, newLocationCapacityModel(capacity))
}
data.ID = types.StringValue(d.name())
resp.Diagnostics.Append(resp.State.Set(ctx, data)...)
}