Skip to content

Commit 24da64a

Browse files
committed
wip
1 parent 9d61263 commit 24da64a

3 files changed

Lines changed: 57 additions & 12 deletions

File tree

internal/loadbalancer/data_source.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,30 @@ func getCommonDataSchema() map[string]*schema.Schema {
5656
Computed: true,
5757
},
5858
"network_id": {
59-
Type: schema.TypeInt,
60-
Computed: true,
59+
Type: schema.TypeInt,
60+
Computed: true,
61+
Deprecated: "This attribute is deprecated. Use hcloud_load_balancer.private_net[].id instead.",
6162
},
6263
"network_ip": {
63-
Type: schema.TypeString,
64+
Type: schema.TypeString,
65+
Computed: true,
66+
Deprecated: "This attribute is deprecated. Use hcloud_load_balancer.private_net[].ipv4 instead.",
67+
},
68+
"private_net": {
69+
Type: schema.TypeList,
6470
Computed: true,
71+
Elem: &schema.Resource{
72+
Schema: map[string]*schema.Schema{
73+
"id": {
74+
Type: schema.TypeInt,
75+
Computed: true,
76+
},
77+
"ipv4": {
78+
Type: schema.TypeString,
79+
Computed: true,
80+
},
81+
},
82+
},
6583
},
6684
"algorithm": {
6785
Type: schema.TypeList,

internal/loadbalancer/resource.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ func getLoadBalancerAttributes(lb *hcloud.LoadBalancer) map[string]any {
399399
"network_zone": lb.Location.NetworkZone,
400400
"labels": lb.Labels,
401401
"target": targetToTerraformTargets(lb.Targets),
402+
"service": serviceToTerraformServices(lb, lb.Services),
403+
"private_net": privateNetToTerraformPrivateNets(lb.PrivateNet),
402404
"delete_protection": lb.Protection.Delete,
403405
}
404406

@@ -410,6 +412,25 @@ func getLoadBalancerAttributes(lb *hcloud.LoadBalancer) map[string]any {
410412
return res
411413
}
412414

415+
func privateNetToTerraformPrivateNets(nets []hcloud.LoadBalancerPrivateNet) []map[string]any {
416+
tfPrivateNets := make([]map[string]any, len(nets))
417+
for i, net := range nets {
418+
tfPrivateNets[i] = map[string]any{
419+
"id": net.Network.ID,
420+
"ipv4": net.IP.String(),
421+
}
422+
}
423+
return tfPrivateNets
424+
}
425+
426+
func serviceToTerraformServices(lb *hcloud.LoadBalancer, services []hcloud.LoadBalancerService) []map[string]any {
427+
tfServices := make([]map[string]any, len(services))
428+
for i, service := range services {
429+
tfServices[i] = getLoadBalancerServiceAttributes(lb, &service)
430+
}
431+
return tfServices
432+
}
433+
413434
func parseTerraformTarget(tfTargets *schema.Set) (opts []hcloud.LoadBalancerCreateOptsTarget) {
414435
for _, _tfTarget := range tfTargets.List() {
415436
tfTarget := _tfTarget.(map[string]any)

internal/loadbalancer/resource_service.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -349,14 +349,18 @@ func resourceLoadBalancerServiceDelete(ctx context.Context, d *schema.ResourceDa
349349
}
350350

351351
func setLoadBalancerServiceSchema(d *schema.ResourceData, lb *hcloud.LoadBalancer, svc *hcloud.LoadBalancerService) {
352-
svcID := fmt.Sprintf("%d__%d", lb.ID, svc.ListenPort)
352+
util.SetSchemaFromAttributes(d, getLoadBalancerServiceAttributes(lb, svc))
353+
}
353354

354-
d.SetId(svcID)
355-
d.Set("load_balancer_id", util.FormatID(lb.ID))
356-
d.Set("protocol", string(svc.Protocol))
357-
d.Set("listen_port", svc.ListenPort)
358-
d.Set("destination_port", svc.DestinationPort)
359-
d.Set("proxyprotocol", svc.Proxyprotocol)
355+
func getLoadBalancerServiceAttributes(lb *hcloud.LoadBalancer, svc *hcloud.LoadBalancerService) map[string]any {
356+
res := map[string]any{
357+
"id": fmt.Sprintf("%d__%d", lb.ID, svc.ListenPort),
358+
"load_balancer_id": util.FormatID(lb.ID),
359+
"protocol": string(svc.Protocol),
360+
"listen_port": svc.ListenPort,
361+
"destination_port": svc.DestinationPort,
362+
"proxyprotocol": svc.Proxyprotocol,
363+
}
360364

361365
if svc.Protocol != hcloud.LoadBalancerServiceProtocolTCP {
362366
httpMap := make(map[string]any)
@@ -381,14 +385,16 @@ func setLoadBalancerServiceSchema(d *schema.ResourceData, lb *hcloud.LoadBalance
381385
}
382386
httpMap["redirect_http"] = svc.HTTP.RedirectHTTP
383387
if len(httpMap) > 0 {
384-
d.Set("http", []any{httpMap})
388+
res["http"] = httpMap
385389
}
386390
}
387391

388392
healthCheck := toTFHealthCheck(svc.HealthCheck)
389393
if len(healthCheck) > 0 {
390-
d.Set("health_check", []any{healthCheck})
394+
res["health_check"] = []any{healthCheck}
391395
}
396+
397+
return res
392398
}
393399

394400
var errInvalidLoadBalancerServiceID = errors.New("invalid load balancer service id")

0 commit comments

Comments
 (0)