/
models.go
94 lines (82 loc) · 3.98 KB
/
models.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
package organization
import (
"context"
fwtypes "github.com/equinix/terraform-provider-equinix/internal/framework/types"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/packethost/packngo"
)
type AddressResourceModel struct {
Address types.String `tfsdk:"address"`
City types.String `tfsdk:"city"`
Country types.String `tfsdk:"country"`
State types.String `tfsdk:"state"`
ZipCode types.String `tfsdk:"zip_code"`
}
type ResourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
Website types.String `tfsdk:"website"`
Twitter types.String `tfsdk:"twitter"`
Logo types.String `tfsdk:"logo"`
Created types.String `tfsdk:"created"`
Updated types.String `tfsdk:"updated"`
Address fwtypes.ListNestedObjectValueOf[AddressResourceModel] `tfsdk:"address"`
}
func (m *ResourceModel) parse(ctx context.Context, org *packngo.Organization) diag.Diagnostics {
m.ID = types.StringValue(org.ID)
m.Name = types.StringValue(org.Name)
m.Description = types.StringValue(org.Description)
m.Website = types.StringValue(org.Website)
m.Twitter = types.StringValue(org.Twitter)
m.Logo = types.StringValue(org.Logo)
m.Created = types.StringValue(org.Created)
m.Updated = types.StringValue(org.Updated)
m.Address = parseAddress(ctx, org.Address)
return nil
}
type DataSourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
OrganizationID types.String `tfsdk:"organization_id"`
Description types.String `tfsdk:"description"`
Website types.String `tfsdk:"website"`
Twitter types.String `tfsdk:"twitter"`
Logo types.String `tfsdk:"logo"`
ProjectIDs []types.List `tfsdk:"project_ids"`
Address fwtypes.ListNestedObjectValueOf[AddressResourceModel] `tfsdk:"address"` // List of Address
}
func (m *DataSourceModel) parse(ctx context.Context, org *packngo.Organization) diag.Diagnostics {
var diags diag.Diagnostics
// Convert Metal Organization data to the Terraform state
m.ID = types.StringValue(org.ID)
m.Name = types.StringValue(org.Name)
m.OrganizationID = types.StringValue(org.ID)
m.Description = types.StringValue(org.Description)
m.Website = types.StringValue(org.Website)
m.Twitter = types.StringValue(org.Twitter)
m.Logo = types.StringValue(org.Logo)
m.Address = parseAddress(ctx, org.Address)
projects := make([]string, len(org.Projects))
pList := make([]basetypes.ListValue, len(org.Projects))
for i, p := range org.Projects {
projects[i] = p.ID
projList, _ := types.ListValueFrom(ctx, types.StringType, p.ID)
pList = append(pList, projList)
}
m.ProjectIDs = pList
return diags
}
func parseAddress(ctx context.Context, addr packngo.Address) fwtypes.ListNestedObjectValueOf[AddressResourceModel] {
addressresourcemodel := make([]AddressResourceModel, 1)
addressresourcemodel[0] = AddressResourceModel{
Address: types.StringValue(addr.Address),
City: types.StringPointerValue(addr.City),
Country: types.StringValue(addr.Country),
State: types.StringPointerValue(addr.State),
ZipCode: types.StringValue(addr.ZipCode),
}
return fwtypes.NewListNestedObjectValueOfValueSlice(ctx, addressresourcemodel)
}