Fix loadbalancer internal models to use TF types (#264)

* Unexport types

* Modify structs to use TF types, uniformize comments

* Adapt to new models

* Adapt to new models

* Adapt tests

* Bug fixes

* Lint fix

* Fix bugs

* Fix wrong reference

Co-authored-by: Vicente Pinto <vicente.pinto@freiheit.com>

* Rename variable

* Rename variable

* Rename variables

---------

Co-authored-by: Henrique Santos <henrique.santos@freiheit.com>
Co-authored-by: Vicente Pinto <vicente.pinto@freiheit.com>
This commit is contained in:
Henrique Santos 2024-02-15 09:55:56 +00:00 committed by GitHub
parent 8b74ec2700
commit 3700ad3018
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 635 additions and 436 deletions

View file

@ -324,7 +324,7 @@ func (r *loadBalancerDataSource) Read(ctx context.Context, req datasource.ReadRe
} }
// Map response body to schema // Map response body to schema
err = mapFields(ctx, lbResp, &model) err = mapFields(lbResp, &model)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading load balancer", fmt.Sprintf("Processing API payload: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading load balancer", fmt.Sprintf("Processing API payload: %v", err))
return return

View file

@ -40,36 +40,34 @@ func TestToCreatePayload(t *testing.T) {
"simple_values_ok", "simple_values_ok",
&Model{ &Model{
ExternalAddress: types.StringValue("external_address"), ExternalAddress: types.StringValue("external_address"),
Listeners: []Listener{ Listeners: types.ListValueMust(types.ObjectType{AttrTypes: listenerTypes}, []attr.Value{
{ types.ObjectValueMust(listenerTypes, map[string]attr.Value{
DisplayName: types.StringValue("display_name"), "display_name": types.StringValue("display_name"),
Port: types.Int64Value(80), "port": types.Int64Value(80),
Protocol: types.StringValue("protocol"), "protocol": types.StringValue("protocol"),
ServerNameIndicators: types.ListValueMust( "server_name_indicators": types.ListValueMust(types.ObjectType{AttrTypes: serverNameIndicatorTypes}, []attr.Value{
types.ObjectType{AttrTypes: serverNameIndicatorTypes}, types.ObjectValueMust(
[]attr.Value{ serverNameIndicatorTypes,
types.ObjectValueMust( map[string]attr.Value{
serverNameIndicatorTypes, "name": types.StringValue("domain.com"),
map[string]attr.Value{ },
"name": types.StringValue("domain.com"), ),
}, },
),
},
), ),
TargetPool: types.StringValue("target_pool"), "target_pool": types.StringValue("target_pool"),
}, }),
}, }),
Name: types.StringValue("name"), Name: types.StringValue("name"),
Networks: []Network{ Networks: types.ListValueMust(types.ObjectType{AttrTypes: networkTypes}, []attr.Value{
{ types.ObjectValueMust(networkTypes, map[string]attr.Value{
NetworkId: types.StringValue("network_id"), "network_id": types.StringValue("network_id"),
Role: types.StringValue("role"), "role": types.StringValue("role"),
}, }),
{ types.ObjectValueMust(networkTypes, map[string]attr.Value{
NetworkId: types.StringValue("network_id_2"), "network_id": types.StringValue("network_id_2"),
Role: types.StringValue("role_2"), "role": types.StringValue("role_2"),
}, }),
}, }),
Options: types.ObjectValueMust( Options: types.ObjectValueMust(
optionsTypes, optionsTypes,
map[string]attr.Value{ map[string]attr.Value{
@ -79,38 +77,32 @@ func TestToCreatePayload(t *testing.T) {
"private_network_only": types.BoolValue(true), "private_network_only": types.BoolValue(true),
}, },
), ),
TargetPools: []TargetPool{ TargetPools: types.ListValueMust(types.ObjectType{AttrTypes: targetPoolTypes}, []attr.Value{
{ types.ObjectValueMust(targetPoolTypes, map[string]attr.Value{
ActiveHealthCheck: types.ObjectValueMust( "active_health_check": types.ObjectValueMust(activeHealthCheckTypes, map[string]attr.Value{
activeHealthCheckTypes, "healthy_threshold": types.Int64Value(1),
map[string]attr.Value{ "interval": types.StringValue("2s"),
"healthy_threshold": types.Int64Value(1), "interval_jitter": types.StringValue("3s"),
"interval": types.StringValue("2s"), "timeout": types.StringValue("4s"),
"interval_jitter": types.StringValue("3s"), "unhealthy_threshold": types.Int64Value(5),
"timeout": types.StringValue("4s"), }),
"unhealthy_threshold": types.Int64Value(5), "name": types.StringValue("name"),
}, "target_port": types.Int64Value(80),
), "targets": types.ListValueMust(types.ObjectType{AttrTypes: targetTypes}, []attr.Value{
Name: types.StringValue("name"), types.ObjectValueMust(targetTypes, map[string]attr.Value{
TargetPort: types.Int64Value(80), "display_name": types.StringValue("display_name"),
Targets: []Target{ "ip": types.StringValue("ip"),
{ }),
DisplayName: types.StringValue("display_name"), }),
Ip: types.StringValue("ip"), "session_persistence": types.ObjectValueMust(sessionPersistenceTypes, map[string]attr.Value{
}, "use_source_ip_address": types.BoolValue(true),
}, }),
SessionPersistence: types.ObjectValueMust( }),
sessionPersistenceTypes, }),
map[string]attr.Value{
"use_source_ip_address": types.BoolValue(true),
},
),
},
},
}, },
&loadbalancer.CreateLoadBalancerPayload{ &loadbalancer.CreateLoadBalancerPayload{
ExternalAddress: utils.Ptr("external_address"), ExternalAddress: utils.Ptr("external_address"),
Listeners: utils.Ptr([]loadbalancer.Listener{ Listeners: &[]loadbalancer.Listener{
{ {
DisplayName: utils.Ptr("display_name"), DisplayName: utils.Ptr("display_name"),
Port: utils.Ptr(int64(80)), Port: utils.Ptr(int64(80)),
@ -122,9 +114,9 @@ func TestToCreatePayload(t *testing.T) {
}, },
TargetPool: utils.Ptr("target_pool"), TargetPool: utils.Ptr("target_pool"),
}, },
}), },
Name: utils.Ptr("name"), Name: utils.Ptr("name"),
Networks: utils.Ptr([]loadbalancer.Network{ Networks: &[]loadbalancer.Network{
{ {
NetworkId: utils.Ptr("network_id"), NetworkId: utils.Ptr("network_id"),
Role: utils.Ptr("role"), Role: utils.Ptr("role"),
@ -133,35 +125,35 @@ func TestToCreatePayload(t *testing.T) {
NetworkId: utils.Ptr("network_id_2"), NetworkId: utils.Ptr("network_id_2"),
Role: utils.Ptr("role_2"), Role: utils.Ptr("role_2"),
}, },
}), },
Options: utils.Ptr(loadbalancer.LoadBalancerOptions{ Options: &loadbalancer.LoadBalancerOptions{
AccessControl: &loadbalancer.LoadbalancerOptionAccessControl{ AccessControl: &loadbalancer.LoadbalancerOptionAccessControl{
AllowedSourceRanges: utils.Ptr([]string{"cidr"}), AllowedSourceRanges: &[]string{"cidr"},
}, },
PrivateNetworkOnly: utils.Ptr(true), PrivateNetworkOnly: utils.Ptr(true),
}), },
TargetPools: utils.Ptr([]loadbalancer.TargetPool{ TargetPools: &[]loadbalancer.TargetPool{
{ {
ActiveHealthCheck: utils.Ptr(loadbalancer.ActiveHealthCheck{ ActiveHealthCheck: &loadbalancer.ActiveHealthCheck{
HealthyThreshold: utils.Ptr(int64(1)), HealthyThreshold: utils.Ptr(int64(1)),
Interval: utils.Ptr("2s"), Interval: utils.Ptr("2s"),
IntervalJitter: utils.Ptr("3s"), IntervalJitter: utils.Ptr("3s"),
Timeout: utils.Ptr("4s"), Timeout: utils.Ptr("4s"),
UnhealthyThreshold: utils.Ptr(int64(5)), UnhealthyThreshold: utils.Ptr(int64(5)),
}), },
Name: utils.Ptr("name"), Name: utils.Ptr("name"),
TargetPort: utils.Ptr(int64(80)), TargetPort: utils.Ptr(int64(80)),
Targets: utils.Ptr([]loadbalancer.Target{ Targets: &[]loadbalancer.Target{
{ {
DisplayName: utils.Ptr("display_name"), DisplayName: utils.Ptr("display_name"),
Ip: utils.Ptr("ip"), Ip: utils.Ptr("ip"),
}, },
}), },
SessionPersistence: utils.Ptr(loadbalancer.SessionPersistence{ SessionPersistence: &loadbalancer.SessionPersistence{
UseSourceIpAddress: utils.Ptr(true), UseSourceIpAddress: utils.Ptr(true),
}), },
}, },
}), },
}, },
true, true,
}, },
@ -194,63 +186,57 @@ func TestToCreatePayload(t *testing.T) {
func TestToTargetPoolUpdatePayload(t *testing.T) { func TestToTargetPoolUpdatePayload(t *testing.T) {
tests := []struct { tests := []struct {
description string description string
input *TargetPool input *targetPool
expected *loadbalancer.UpdateTargetPoolPayload expected *loadbalancer.UpdateTargetPoolPayload
isValid bool isValid bool
}{ }{
{ {
"default_values_ok", "default_values_ok",
&TargetPool{}, &targetPool{},
&loadbalancer.UpdateTargetPoolPayload{}, &loadbalancer.UpdateTargetPoolPayload{},
true, true,
}, },
{ {
"simple_values_ok", "simple_values_ok",
&TargetPool{ &targetPool{
ActiveHealthCheck: types.ObjectValueMust( ActiveHealthCheck: types.ObjectValueMust(activeHealthCheckTypes, map[string]attr.Value{
activeHealthCheckTypes, "healthy_threshold": types.Int64Value(1),
map[string]attr.Value{ "interval": types.StringValue("2s"),
"healthy_threshold": types.Int64Value(1), "interval_jitter": types.StringValue("3s"),
"interval": types.StringValue("2s"), "timeout": types.StringValue("4s"),
"interval_jitter": types.StringValue("3s"), "unhealthy_threshold": types.Int64Value(5),
"timeout": types.StringValue("4s"), }),
"unhealthy_threshold": types.Int64Value(5),
},
),
Name: types.StringValue("name"), Name: types.StringValue("name"),
TargetPort: types.Int64Value(80), TargetPort: types.Int64Value(80),
Targets: []Target{ Targets: types.ListValueMust(types.ObjectType{AttrTypes: targetTypes}, []attr.Value{
{ types.ObjectValueMust(targetTypes, map[string]attr.Value{
DisplayName: types.StringValue("display_name"), "display_name": types.StringValue("display_name"),
Ip: types.StringValue("ip"), "ip": types.StringValue("ip"),
}, }),
}, }),
SessionPersistence: types.ObjectValueMust( SessionPersistence: types.ObjectValueMust(sessionPersistenceTypes, map[string]attr.Value{
sessionPersistenceTypes, "use_source_ip_address": types.BoolValue(false),
map[string]attr.Value{ }),
"use_source_ip_address": types.BoolValue(false),
},
),
}, },
&loadbalancer.UpdateTargetPoolPayload{ &loadbalancer.UpdateTargetPoolPayload{
ActiveHealthCheck: utils.Ptr(loadbalancer.ActiveHealthCheck{ ActiveHealthCheck: &loadbalancer.ActiveHealthCheck{
HealthyThreshold: utils.Ptr(int64(1)), HealthyThreshold: utils.Ptr(int64(1)),
Interval: utils.Ptr("2s"), Interval: utils.Ptr("2s"),
IntervalJitter: utils.Ptr("3s"), IntervalJitter: utils.Ptr("3s"),
Timeout: utils.Ptr("4s"), Timeout: utils.Ptr("4s"),
UnhealthyThreshold: utils.Ptr(int64(5)), UnhealthyThreshold: utils.Ptr(int64(5)),
}), },
Name: utils.Ptr("name"), Name: utils.Ptr("name"),
TargetPort: utils.Ptr(int64(80)), TargetPort: utils.Ptr(int64(80)),
Targets: utils.Ptr([]loadbalancer.Target{ Targets: &[]loadbalancer.Target{
{ {
DisplayName: utils.Ptr("display_name"), DisplayName: utils.Ptr("display_name"),
Ip: utils.Ptr("ip"), Ip: utils.Ptr("ip"),
}, },
}), },
SessionPersistence: utils.Ptr(loadbalancer.SessionPersistence{ SessionPersistence: &loadbalancer.SessionPersistence{
UseSourceIpAddress: utils.Ptr(false), UseSourceIpAddress: utils.Ptr(false),
}), },
}, },
true, true,
}, },
@ -303,9 +289,18 @@ func TestMapFields(t *testing.T) {
TargetPools: nil, TargetPools: nil,
}, },
&Model{ &Model{
Id: types.StringValue("pid,name"), Id: types.StringValue("pid,name"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Name: types.StringValue("name"), ExternalAddress: types.StringNull(),
Listeners: types.ListNull(types.ObjectType{AttrTypes: listenerTypes}),
Name: types.StringValue("name"),
Networks: types.ListNull(types.ObjectType{AttrTypes: networkTypes}),
Options: types.ObjectValueMust(optionsTypes, map[string]attr.Value{
"acl": types.SetNull(types.StringType),
"private_network_only": types.BoolNull(),
}),
PrivateAddress: types.StringNull(),
TargetPools: types.ListNull(types.ObjectType{AttrTypes: targetPoolTypes}),
}, },
true, true,
}, },
@ -370,37 +365,35 @@ func TestMapFields(t *testing.T) {
&Model{ &Model{
Id: types.StringValue("pid,name"), Id: types.StringValue("pid,name"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Name: types.StringValue("name"),
ExternalAddress: types.StringValue("external_address"), ExternalAddress: types.StringValue("external_address"),
Listeners: []Listener{ Listeners: types.ListValueMust(types.ObjectType{AttrTypes: listenerTypes}, []attr.Value{
{ types.ObjectValueMust(listenerTypes, map[string]attr.Value{
DisplayName: types.StringValue("display_name"), "display_name": types.StringValue("display_name"),
Port: types.Int64Value(80), "port": types.Int64Value(80),
Protocol: types.StringValue("protocol"), "protocol": types.StringValue("protocol"),
ServerNameIndicators: types.ListValueMust( "server_name_indicators": types.ListValueMust(types.ObjectType{AttrTypes: serverNameIndicatorTypes}, []attr.Value{
types.ObjectType{AttrTypes: serverNameIndicatorTypes}, types.ObjectValueMust(
[]attr.Value{ serverNameIndicatorTypes,
types.ObjectValueMust( map[string]attr.Value{
serverNameIndicatorTypes, "name": types.StringValue("domain.com"),
map[string]attr.Value{ },
"name": types.StringValue("domain.com"), ),
}, },
),
},
), ),
TargetPool: types.StringValue("target_pool"), "target_pool": types.StringValue("target_pool"),
}, }),
}, }),
Networks: []Network{ Name: types.StringValue("name"),
{ Networks: types.ListValueMust(types.ObjectType{AttrTypes: networkTypes}, []attr.Value{
NetworkId: types.StringValue("network_id"), types.ObjectValueMust(networkTypes, map[string]attr.Value{
Role: types.StringValue("role"), "network_id": types.StringValue("network_id"),
}, "role": types.StringValue("role"),
{ }),
NetworkId: types.StringValue("network_id_2"), types.ObjectValueMust(networkTypes, map[string]attr.Value{
Role: types.StringValue("role_2"), "network_id": types.StringValue("network_id_2"),
}, "role": types.StringValue("role_2"),
}, }),
}),
Options: types.ObjectValueMust( Options: types.ObjectValueMust(
optionsTypes, optionsTypes,
map[string]attr.Value{ map[string]attr.Value{
@ -410,35 +403,28 @@ func TestMapFields(t *testing.T) {
"private_network_only": types.BoolValue(true), "private_network_only": types.BoolValue(true),
}, },
), ),
TargetPools: []TargetPool{ TargetPools: types.ListValueMust(types.ObjectType{AttrTypes: targetPoolTypes}, []attr.Value{
{ types.ObjectValueMust(targetPoolTypes, map[string]attr.Value{
ActiveHealthCheck: types.ObjectValueMust( "active_health_check": types.ObjectValueMust(activeHealthCheckTypes, map[string]attr.Value{
activeHealthCheckTypes, "healthy_threshold": types.Int64Value(1),
map[string]attr.Value{ "interval": types.StringValue("2s"),
"healthy_threshold": types.Int64Value(1), "interval_jitter": types.StringValue("3s"),
"interval": types.StringValue("2s"), "timeout": types.StringValue("4s"),
"interval_jitter": types.StringValue("3s"), "unhealthy_threshold": types.Int64Value(5),
"timeout": types.StringValue("4s"), }),
"name": types.StringValue("name"),
"unhealthy_threshold": types.Int64Value(5), "target_port": types.Int64Value(80),
}, "targets": types.ListValueMust(types.ObjectType{AttrTypes: targetTypes}, []attr.Value{
), types.ObjectValueMust(targetTypes, map[string]attr.Value{
Name: types.StringValue("name"), "display_name": types.StringValue("display_name"),
TargetPort: types.Int64Value(80), "ip": types.StringValue("ip"),
Targets: []Target{ }),
{ }),
DisplayName: types.StringValue("display_name"), "session_persistence": types.ObjectValueMust(sessionPersistenceTypes, map[string]attr.Value{
Ip: types.StringValue("ip"), "use_source_ip_address": types.BoolValue(true),
}, }),
}, }),
SessionPersistence: types.ObjectValueMust( }),
sessionPersistenceTypes,
map[string]attr.Value{
"use_source_ip_address": types.BoolValue(true),
},
),
},
},
}, },
true, true,
}, },
@ -460,7 +446,7 @@ func TestMapFields(t *testing.T) {
model := &Model{ model := &Model{
ProjectId: tt.expected.ProjectId, ProjectId: tt.expected.ProjectId,
} }
err := mapFields(context.Background(), tt.input, model) err := mapFields(tt.input, model)
if !tt.isValid && err == nil { if !tt.isValid && err == nil {
t.Fatalf("Should have failed") t.Fatalf("Should have failed")
} }