/
models.go
127 lines (111 loc) · 5.55 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
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
package project
import (
"context"
"github.com/equinix/equinix-sdk-go/services/metalv1"
"path"
"strings"
"time"
fwtypes "github.com/equinix/terraform-provider-equinix/internal/framework/types"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
)
type ResourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Created types.String `tfsdk:"created"`
Updated types.String `tfsdk:"updated"`
BackendTransfer types.Bool `tfsdk:"backend_transfer"`
PaymentMethodID types.String `tfsdk:"payment_method_id"`
OrganizationID types.String `tfsdk:"organization_id"`
BGPConfig fwtypes.ListNestedObjectValueOf[BGPConfigModel] `tfsdk:"bgp_config"`
}
type DataSourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
ProjectID types.String `tfsdk:"project_id"`
Created types.String `tfsdk:"created"`
Updated types.String `tfsdk:"updated"`
BackendTransfer types.Bool `tfsdk:"backend_transfer"`
PaymentMethodID types.String `tfsdk:"payment_method_id"`
OrganizationID types.String `tfsdk:"organization_id"`
UserIDs types.List `tfsdk:"user_ids"`
BGPConfig fwtypes.ListNestedObjectValueOf[BGPConfigModel] `tfsdk:"bgp_config"`
}
type BGPConfigModel struct {
DeploymentType types.String `tfsdk:"deployment_type"`
ASN types.Int64 `tfsdk:"asn"`
MD5 types.String `tfsdk:"md5"`
Status types.String `tfsdk:"status"`
MaxPrefix types.Int64 `tfsdk:"max_prefix"`
}
func (m *ResourceModel) parse(ctx context.Context, project *metalv1.Project, bgpConfig *metalv1.BgpConfig) diag.Diagnostics {
var diags diag.Diagnostics
m.ID = types.StringValue(project.GetId())
m.Name = types.StringValue(project.GetName())
m.Created = types.StringValue(project.GetCreatedAt().Format(time.RFC3339))
m.Updated = types.StringValue(project.GetUpdatedAt().Format(time.RFC3339))
m.BackendTransfer = types.BoolValue(project.AdditionalProperties["backend_transfer_enabled"].(bool)) // No backend_transfer_enabled property in API spec
m.OrganizationID = types.StringValue(path.Base(project.Organization.AdditionalProperties["href"].(string)))
m.PaymentMethodID = types.StringValue("")
if len(project.PaymentMethod.GetHref()) != 0 {
newValue := path.Base(project.PaymentMethod.GetHref())
if !strings.EqualFold(strings.Trim(m.PaymentMethodID.ValueString(), `"`), strings.Trim(newValue, `"`)) {
m.PaymentMethodID = types.StringValue(path.Base(project.PaymentMethod.GetHref()))
}
}
// Handle BGP Config if present
m.BGPConfig = parseBGPConfig(ctx, bgpConfig)
return diags
}
func (m *DataSourceModel) parse(ctx context.Context, project *metalv1.Project, bgpConfig *metalv1.BgpConfig) diag.Diagnostics {
var diags diag.Diagnostics
m.ID = types.StringValue(project.GetId())
m.ProjectID = types.StringValue(project.GetId())
m.Name = types.StringValue(project.GetName())
m.Created = types.StringValue(project.GetCreatedAt().Format(time.RFC3339))
m.Updated = types.StringValue(project.GetUpdatedAt().Format(time.RFC3339))
m.BackendTransfer = types.BoolValue(project.AdditionalProperties["backend_transfer_enabled"].(bool)) // No backend_transfer_enabled property in API spec
m.OrganizationID = types.StringValue(path.Base(project.Organization.AdditionalProperties["href"].(string)))
m.PaymentMethodID = types.StringValue("")
if len(project.PaymentMethod.GetHref()) != 0 {
m.PaymentMethodID = types.StringValue(path.Base(project.PaymentMethod.GetHref()))
}
// Parse User IDs
projUserIds := []string{}
for _, u := range project.GetMembers() {
projUserIds = append(projUserIds, path.Base(u.GetHref()))
}
userIDs, diags := types.ListValueFrom(ctx, types.StringType, projUserIds)
if diags.HasError() {
return diags
}
m.UserIDs = userIDs
// Handle BGP Config if present
m.BGPConfig = parseBGPConfig(ctx, bgpConfig)
return diags
}
func parseBGPConfig(ctx context.Context, bgpConfig *metalv1.BgpConfig) fwtypes.ListNestedObjectValueOf[BGPConfigModel] {
if !isEmptyMetalBGPConfig(bgpConfig) {
bgpConfigResourceModel := make([]BGPConfigModel, 1)
bgpConfigResourceModel[0] = BGPConfigModel{
DeploymentType: types.StringValue(string(bgpConfig.GetDeploymentType())),
ASN: types.Int64Value(int64(bgpConfig.GetAsn())),
Status: types.StringValue(string(bgpConfig.GetStatus())),
MaxPrefix: types.Int64Value(int64(bgpConfig.GetMaxPrefix())),
}
if bgpConfig.Md5.Get() != nil {
bgpConfigResourceModel[0].MD5 = types.StringValue(bgpConfig.GetMd5())
}
return fwtypes.NewListNestedObjectValueOfValueSlice[BGPConfigModel](ctx, bgpConfigResourceModel)
}
return fwtypes.NewListNestedObjectValueOfNull[BGPConfigModel](ctx)
}
// isEmptyBGPConfig checks if the provided BgpConfig is considered empty
func isEmptyMetalBGPConfig(bgp *metalv1.BgpConfig) bool {
if metalv1.IsNil(bgp) {
return true
}
return metalv1.IsNil(bgp.DeploymentType) &&
metalv1.IsNil(bgp.Asn) &&
metalv1.IsNil(bgp.Status)
}