Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(utils): MarshalID & UnmarshalID to utils package #420

Merged
merged 5 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions internal/service/kubernetes/node_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func resourceNodeGroupCreate(ctx context.Context, d *schema.ResourceData, meta i
if err != nil {
return diag.FromErr(err)
}
d.SetId(marshalID(clusterID, ng.Name))
d.SetId(utils.MarshalID(clusterID, ng.Name))

ng, err = svc.WaitForKubernetesNodeGroupState(ctx, &request.WaitForKubernetesNodeGroupStateRequest{
DesiredState: upcloud.KubernetesNodeGroupStateRunning,
Expand All @@ -225,7 +225,7 @@ func resourceNodeGroupCreate(ctx context.Context, d *schema.ResourceData, meta i
func resourceNodeGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
svc := meta.(*service.Service)
var clusterID, name string
if err := unmarshalID(d.Id(), &clusterID, &name); err != nil {
if err := utils.UnmarshalID(d.Id(), &clusterID, &name); err != nil {
return diag.FromErr(err)
}
ng, err := svc.GetKubernetesNodeGroup(ctx, &request.GetKubernetesNodeGroupRequest{
Expand All @@ -235,7 +235,7 @@ func resourceNodeGroupRead(ctx context.Context, d *schema.ResourceData, meta int
if err != nil {
return utils.HandleResourceError(d.Get("name").(string), d, err)
}
d.SetId(marshalID(clusterID, ng.Name))
d.SetId(utils.MarshalID(clusterID, ng.Name))
return setNodeGroupResourceData(d, clusterID, &ng.KubernetesNodeGroup)
}

Expand All @@ -245,7 +245,7 @@ func resourceNodeGroupUpdate(ctx context.Context, d *schema.ResourceData, meta i
}
svc := meta.(*service.Service)
var clusterID, name string
if err := unmarshalID(d.Id(), &clusterID, &name); err != nil {
if err := utils.UnmarshalID(d.Id(), &clusterID, &name); err != nil {
return diag.FromErr(err)
}
ng, err := svc.ModifyKubernetesNodeGroup(ctx, &request.ModifyKubernetesNodeGroupRequest{
Expand Down Expand Up @@ -275,7 +275,7 @@ func resourceNodeGroupUpdate(ctx context.Context, d *schema.ResourceData, meta i
func resourceNodeGroupDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
svc := meta.(*service.Service)
var clusterID, name string
if err := unmarshalID(d.Id(), &clusterID, &name); err != nil {
if err := utils.UnmarshalID(d.Id(), &clusterID, &name); err != nil {
return diag.FromErr(err)
}
err := svc.DeleteKubernetesNodeGroup(ctx, &request.DeleteKubernetesNodeGroupRequest{
Expand Down
17 changes: 0 additions & 17 deletions internal/service/kubernetes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,10 @@ package kubernetes

import (
"encoding/base64"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func marshalID(components ...string) string {
return strings.Join(components, "/")
}

func unmarshalID(id string, components ...*string) error {
parts := strings.Split(id, "/")
if len(parts) > len(components) {
return fmt.Errorf("not enough components (%d) to unmarshal id '%s'", len(components), id)
}
for i, c := range parts {
*components[i] = c
}
return nil
}

func setB64Decoded(d *schema.ResourceData, field string, value string) error {
decoded, err := base64.StdEncoding.DecodeString(value)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions internal/service/loadbalancer/backend.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package loadbalancer

Check failure on line 1 in internal/service/loadbalancer/backend.go

View workflow job for this annotation

GitHub Actions / golangci-lint

: # github.com/UpCloudLtd/terraform-provider-upcloud/internal/service/loadbalancer

Check failure on line 1 in internal/service/loadbalancer/backend.go

View workflow job for this annotation

GitHub Actions / golangci-lint

: # github.com/UpCloudLtd/terraform-provider-upcloud/internal/service/loadbalancer

import (
"context"
Expand Down Expand Up @@ -81,7 +81,7 @@
return diag.FromErr(err)
}

d.SetId(marshalID(serviceID, be.Name))
d.SetId(utils.MarshalID(serviceID, be.Name))

if diags = setBackendResourceData(d, be); len(diags) > 0 {
return diags
Expand All @@ -94,7 +94,7 @@
func resourceBackendRead(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
svc := meta.(*service.Service)
var serviceID, name string
if err := unmarshalID(d.Id(), &serviceID, &name); err != nil {
if err := utils.UnmarshalID(d.Id(), &serviceID, &name); err != nil {
return diag.FromErr(err)
}
be, err := svc.GetLoadBalancerBackend(ctx, &request.GetLoadBalancerBackendRequest{
Expand All @@ -105,7 +105,7 @@
return utils.HandleResourceError(d.Get("name").(string), d, err)
}

d.SetId(marshalID(serviceID, be.Name))
d.SetId(utils.MarshalID(serviceID, be.Name))

if err = d.Set("loadbalancer", serviceID); err != nil {
return diag.FromErr(err)
Expand All @@ -121,7 +121,7 @@
func resourceBackendUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
svc := meta.(*service.Service)
var serviceID, name string
if err := unmarshalID(d.Id(), &serviceID, &name); err != nil {
if err := utils.UnmarshalID(d.Id(), &serviceID, &name); err != nil {
return diag.FromErr(err)
}

Expand All @@ -138,7 +138,7 @@
return diag.FromErr(err)
}

d.SetId(marshalID(d.Get("loadbalancer").(string), be.Name))
d.SetId(utils.MarshalID(d.Get("loadbalancer").(string), be.Name))

if diags = setBackendResourceData(d, be); len(diags) > 0 {
return diags
Expand All @@ -151,7 +151,7 @@
func resourceBackendDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
svc := meta.(*service.Service)
var serviceID, name string
if err := unmarshalID(d.Id(), &serviceID, &name); err != nil {
if err := utils.UnmarshalID(d.Id(), &serviceID, &name); err != nil {
return diag.FromErr(err)
}
tflog.Info(ctx, "deleting backend", map[string]interface{}{"name": name, "service_uuid": serviceID})
Expand Down
16 changes: 8 additions & 8 deletions internal/service/loadbalancer/backend_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func resourceBackendMemberCreateFunc(memberType upcloud.LoadBalancerBackendMembe
return func(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
svc := meta.(*service.Service)
var serviceID, beName string
if err := unmarshalID(d.Get("backend").(string), &serviceID, &beName); err != nil {
if err := utils.UnmarshalID(d.Get("backend").(string), &serviceID, &beName); err != nil {
return diag.FromErr(err)
}
member, err := svc.CreateLoadBalancerBackendMember(ctx, &request.CreateLoadBalancerBackendMemberRequest{
Expand All @@ -161,7 +161,7 @@ func resourceBackendMemberCreateFunc(memberType upcloud.LoadBalancerBackendMembe
return diags
}

d.SetId(marshalID(serviceID, beName, member.Name))
d.SetId(utils.MarshalID(serviceID, beName, member.Name))

tflog.Info(ctx, "backend member created", map[string]interface{}{"name": member.Name, "service_uuid": serviceID, "be_name": beName})
return diags
Expand All @@ -171,7 +171,7 @@ func resourceBackendMemberCreateFunc(memberType upcloud.LoadBalancerBackendMembe
func resourceBackendMemberRead(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
svc := meta.(*service.Service)
var serviceID, beName, name string
if err := unmarshalID(d.Id(), &serviceID, &beName, &name); err != nil {
if err := utils.UnmarshalID(d.Id(), &serviceID, &beName, &name); err != nil {
return diag.FromErr(err)
}
member, err := svc.GetLoadBalancerBackendMember(ctx, &request.GetLoadBalancerBackendMemberRequest{
Expand All @@ -183,9 +183,9 @@ func resourceBackendMemberRead(ctx context.Context, d *schema.ResourceData, meta
return utils.HandleResourceError(d.Get("name").(string), d, err)
}

d.SetId(marshalID(serviceID, beName, member.Name))
d.SetId(utils.MarshalID(serviceID, beName, member.Name))

if err = d.Set("backend", marshalID(serviceID, beName)); err != nil {
if err = d.Set("backend", utils.MarshalID(serviceID, beName)); err != nil {
return diag.FromErr(err)
}

Expand All @@ -199,7 +199,7 @@ func resourceBackendMemberRead(ctx context.Context, d *schema.ResourceData, meta
func resourceBackendMemberUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
svc := meta.(*service.Service)
var serviceID, beName, name string
if err := unmarshalID(d.Id(), &serviceID, &beName, &name); err != nil {
if err := utils.UnmarshalID(d.Id(), &serviceID, &beName, &name); err != nil {
return diag.FromErr(err)
}
member, err := svc.ModifyLoadBalancerBackendMember(ctx, &request.ModifyLoadBalancerBackendMemberRequest{
Expand All @@ -219,7 +219,7 @@ func resourceBackendMemberUpdate(ctx context.Context, d *schema.ResourceData, me
return diag.FromErr(err)
}

d.SetId(marshalID(serviceID, beName, member.Name))
d.SetId(utils.MarshalID(serviceID, beName, member.Name))

if diags = setBackendMemberResourceData(d, member); len(diags) > 0 {
return diags
Expand All @@ -232,7 +232,7 @@ func resourceBackendMemberUpdate(ctx context.Context, d *schema.ResourceData, me
func resourceBackendMemberDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
svc := meta.(*service.Service)
var serviceID, beName, name string
if err := unmarshalID(d.Id(), &serviceID, &beName, &name); err != nil {
if err := utils.UnmarshalID(d.Id(), &serviceID, &beName, &name); err != nil {
return diag.FromErr(err)
}
tflog.Info(ctx, "deleting backend member", map[string]interface{}{"name": name, "service_uuid": serviceID, "be_name": beName})
Expand Down
12 changes: 6 additions & 6 deletions internal/service/loadbalancer/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func resourceFrontendCreate(ctx context.Context, d *schema.ResourceData, meta in
return diag.FromErr(err)
}

d.SetId(marshalID(serviceID, fe.Name))
d.SetId(utils.MarshalID(serviceID, fe.Name))

if diags = setFrontendResourceData(d, fe); len(diags) > 0 {
return diags
Expand All @@ -138,7 +138,7 @@ func resourceFrontendCreate(ctx context.Context, d *schema.ResourceData, meta in
func resourceFrontendRead(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
svc := meta.(*service.Service)
var serviceID, name string
if err := unmarshalID(d.Id(), &serviceID, &name); err != nil {
if err := utils.UnmarshalID(d.Id(), &serviceID, &name); err != nil {
return diag.FromErr(err)
}
fe, err := svc.GetLoadBalancerFrontend(ctx, &request.GetLoadBalancerFrontendRequest{
Expand All @@ -149,7 +149,7 @@ func resourceFrontendRead(ctx context.Context, d *schema.ResourceData, meta inte
return utils.HandleResourceError(d.Get("name").(string), d, err)
}

d.SetId(marshalID(serviceID, fe.Name))
d.SetId(utils.MarshalID(serviceID, fe.Name))

if err = d.Set("loadbalancer", serviceID); err != nil {
return diag.FromErr(err)
Expand All @@ -165,7 +165,7 @@ func resourceFrontendRead(ctx context.Context, d *schema.ResourceData, meta inte
func resourceFrontendUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
svc := meta.(*service.Service)
var serviceID, name string
if err := unmarshalID(d.Id(), &serviceID, &name); err != nil {
if err := utils.UnmarshalID(d.Id(), &serviceID, &name); err != nil {
return diag.FromErr(err)
}
fe, err := svc.ModifyLoadBalancerFrontend(ctx, &request.ModifyLoadBalancerFrontendRequest{
Expand All @@ -183,7 +183,7 @@ func resourceFrontendUpdate(ctx context.Context, d *schema.ResourceData, meta in
return diag.FromErr(err)
}

d.SetId(marshalID(d.Get("loadbalancer").(string), fe.Name))
d.SetId(utils.MarshalID(d.Get("loadbalancer").(string), fe.Name))

if diags = setFrontendResourceData(d, fe); len(diags) > 0 {
return diags
Expand All @@ -196,7 +196,7 @@ func resourceFrontendUpdate(ctx context.Context, d *schema.ResourceData, meta in
func resourceFrontendDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
svc := meta.(*service.Service)
var serviceID, name string
if err := unmarshalID(d.Id(), &serviceID, &name); err != nil {
if err := utils.UnmarshalID(d.Id(), &serviceID, &name); err != nil {
return diag.FromErr(err)
}
tflog.Info(ctx, "deleting frontend", map[string]interface{}{"name": name, "service_uuid": serviceID})
Expand Down
16 changes: 8 additions & 8 deletions internal/service/loadbalancer/frontend_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func resourceFrontendRuleCreate(ctx context.Context, d *schema.ResourceData, met
}

var serviceID, feName string
if err := unmarshalID(d.Get("frontend").(string), &serviceID, &feName); err != nil {
if err := utils.UnmarshalID(d.Get("frontend").(string), &serviceID, &feName); err != nil {
return diag.FromErr(err)
}

Expand All @@ -103,7 +103,7 @@ func resourceFrontendRuleCreate(ctx context.Context, d *schema.ResourceData, met
return diag.FromErr(err)
}

d.SetId(marshalID(serviceID, feName, rule.Name))
d.SetId(utils.MarshalID(serviceID, feName, rule.Name))

if diags = setFrontendRuleResourceData(d, rule); len(diags) > 0 {
return diags
Expand All @@ -116,7 +116,7 @@ func resourceFrontendRuleCreate(ctx context.Context, d *schema.ResourceData, met
func resourceFrontendRuleRead(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
svc := meta.(*service.Service)
var serviceID, feName, name string
if err := unmarshalID(d.Id(), &serviceID, &feName, &name); err != nil {
if err := utils.UnmarshalID(d.Id(), &serviceID, &feName, &name); err != nil {
return diag.FromErr(err)
}
rule, err := svc.GetLoadBalancerFrontendRule(ctx, &request.GetLoadBalancerFrontendRuleRequest{
Expand All @@ -128,9 +128,9 @@ func resourceFrontendRuleRead(ctx context.Context, d *schema.ResourceData, meta
return utils.HandleResourceError(d.Get("name").(string), d, err)
}

d.SetId(marshalID(serviceID, feName, rule.Name))
d.SetId(utils.MarshalID(serviceID, feName, rule.Name))

if err = d.Set("frontend", marshalID(serviceID, feName)); err != nil {
if err = d.Set("frontend", utils.MarshalID(serviceID, feName)); err != nil {
return diag.FromErr(err)
}

Expand All @@ -144,7 +144,7 @@ func resourceFrontendRuleRead(ctx context.Context, d *schema.ResourceData, meta
func resourceFrontendRuleUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
svc := meta.(*service.Service)
var serviceID, feName, name string
if err := unmarshalID(d.Id(), &serviceID, &feName, &name); err != nil {
if err := utils.UnmarshalID(d.Id(), &serviceID, &feName, &name); err != nil {
return diag.FromErr(err)
}
// name and priority fields doesn't force replacement and can be updated in-place
Expand All @@ -162,7 +162,7 @@ func resourceFrontendRuleUpdate(ctx context.Context, d *schema.ResourceData, met
return diag.FromErr(err)
}

d.SetId(marshalID(serviceID, feName, rule.Name))
d.SetId(utils.MarshalID(serviceID, feName, rule.Name))

if diags = setFrontendRuleResourceData(d, rule); len(diags) > 0 {
return diags
Expand All @@ -175,7 +175,7 @@ func resourceFrontendRuleUpdate(ctx context.Context, d *schema.ResourceData, met
func resourceFrontendRuleDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
svc := meta.(*service.Service)
var serviceID, feName, name string
if err := unmarshalID(d.Id(), &serviceID, &feName, &name); err != nil {
if err := utils.UnmarshalID(d.Id(), &serviceID, &feName, &name); err != nil {
return diag.FromErr(err)
}

Expand Down
14 changes: 7 additions & 7 deletions internal/service/loadbalancer/frontend_tls_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func ResourceFrontendTLSConfig() *schema.Resource {
func resourceFrontendTLSConfigCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
svc := meta.(*service.Service)
var serviceID, feName string
if err := unmarshalID(d.Get("frontend").(string), &serviceID, &feName); err != nil {
if err := utils.UnmarshalID(d.Get("frontend").(string), &serviceID, &feName); err != nil {
return diag.FromErr(err)
}
t, err := svc.CreateLoadBalancerFrontendTLSConfig(ctx, &request.CreateLoadBalancerFrontendTLSConfigRequest{
Expand All @@ -62,7 +62,7 @@ func resourceFrontendTLSConfigCreate(ctx context.Context, d *schema.ResourceData
return diag.FromErr(err)
}

d.SetId(marshalID(serviceID, feName, t.Name))
d.SetId(utils.MarshalID(serviceID, feName, t.Name))

if diags = setFrontendTLSConfigResourceData(d, t); len(diags) > 0 {
return diags
Expand All @@ -75,7 +75,7 @@ func resourceFrontendTLSConfigCreate(ctx context.Context, d *schema.ResourceData
func resourceFrontendTLSConfigRead(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
svc := meta.(*service.Service)
var serviceID, feName, name string
if err := unmarshalID(d.Id(), &serviceID, &feName, &name); err != nil {
if err := utils.UnmarshalID(d.Id(), &serviceID, &feName, &name); err != nil {
return diag.FromErr(err)
}
t, err := svc.GetLoadBalancerFrontendTLSConfig(ctx, &request.GetLoadBalancerFrontendTLSConfigRequest{
Expand All @@ -87,7 +87,7 @@ func resourceFrontendTLSConfigRead(ctx context.Context, d *schema.ResourceData,
return utils.HandleResourceError(d.Get("name").(string), d, err)
}

if err = d.Set("frontend", marshalID(serviceID, feName)); err != nil {
if err = d.Set("frontend", utils.MarshalID(serviceID, feName)); err != nil {
return diag.FromErr(err)
}

Expand All @@ -101,7 +101,7 @@ func resourceFrontendTLSConfigRead(ctx context.Context, d *schema.ResourceData,
func resourceFrontendTLSConfigUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
svc := meta.(*service.Service)
var serviceID, feName, name string
if err := unmarshalID(d.Id(), &serviceID, &feName, &name); err != nil {
if err := utils.UnmarshalID(d.Id(), &serviceID, &feName, &name); err != nil {
return diag.FromErr(err)
}
t, err := svc.ModifyLoadBalancerFrontendTLSConfig(ctx, &request.ModifyLoadBalancerFrontendTLSConfigRequest{
Expand All @@ -117,7 +117,7 @@ func resourceFrontendTLSConfigUpdate(ctx context.Context, d *schema.ResourceData
return diag.FromErr(err)
}

d.SetId(marshalID(serviceID, feName, t.Name))
d.SetId(utils.MarshalID(serviceID, feName, t.Name))

if diags = setFrontendTLSConfigResourceData(d, t); len(diags) > 0 {
return diags
Expand All @@ -130,7 +130,7 @@ func resourceFrontendTLSConfigUpdate(ctx context.Context, d *schema.ResourceData
func resourceFrontendTLSConfigDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
svc := meta.(*service.Service)
var serviceID, feName, name string
if err := unmarshalID(d.Id(), &serviceID, &feName, &name); err != nil {
if err := utils.UnmarshalID(d.Id(), &serviceID, &feName, &name); err != nil {
return diag.FromErr(err)
}
tflog.Info(ctx, "deleting frontend TLS config", map[string]interface{}{"name": name, "service_uuid": serviceID, "fe_name": feName})
Expand Down
4 changes: 2 additions & 2 deletions internal/service/loadbalancer/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func resourceLoadBalancerNetworkUpdate(ctx context.Context, d *schema.ResourceDa
if id, ok = d.Get(idKey).(string); !ok {
return diag.FromErr(fmt.Errorf("unable to determine network ID %s", idKey))
}
if err := unmarshalID(id, &serviceID, &networkName); err != nil {
if err := utils.UnmarshalID(id, &serviceID, &networkName); err != nil {
return diag.FromErr(err)
}
req := &request.ModifyLoadBalancerNetworkRequest{
Expand Down Expand Up @@ -462,7 +462,7 @@ func setLoadBalancerNetworkResourceData(d *schema.ResourceData, lb *upcloud.Load
"family": string(net.Family),
"network": net.UUID,
"dns_name": net.DNSName,
"id": marshalID(lb.UUID, net.Name),
"id": utils.MarshalID(lb.UUID, net.Name),
})
}
if err := d.Set("networks", networks); err != nil {
Expand Down
Loading
Loading