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

Allow alias usage for the domain and system owner #555

Merged
merged 6 commits into from
Dec 24, 2024
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
4 changes: 4 additions & 0 deletions .changes/unreleased/Bugfix-20241224-105420.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
kind: Bugfix
body: Allow opslevel_domain and opslevel_system resources to specify an alias for their owner
for the owner
time: 2024-12-24T10:54:20.871486-06:00
2 changes: 1 addition & 1 deletion examples/resources/opslevel_domain/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ data "opslevel_team" "devs" {
resource "opslevel_domain" "example" {
name = "Example"
description = "The whole app in one monolith"
owner = data.opslevel_team.devs.id // or .alias
owner = data.opslevel_team.devs.id // or .aliases[0]
note = "This is an example"
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ require (
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241104194629-dd2ea8efbc28 // indirect
google.golang.org/grpc v1.68.0 // indirect
google.golang.org/protobuf v1.35.1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241223144023-3abc09e42ca8 // indirect
google.golang.org/grpc v1.69.2 // indirect
google.golang.org/protobuf v1.36.1 // indirect
)

// replace github.com/opslevel/opslevel-go/v2024 => ./submodules/opslevel-go
26 changes: 20 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/gabriel-vasile/mimetype v1.4.7 h1:SKFKl7kD0RiPdbht0s7hFtjl489WcQ1VyPW8ZzUMYCA=
github.com/gabriel-vasile/mimetype v1.4.7/go.mod h1:GDlAgAyIRT27BhFl53XNAFtfjzOkLaF35JdEG0P7LtU=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
Expand Down Expand Up @@ -119,6 +123,16 @@ github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IU
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
go.opentelemetry.io/otel v1.31.0 h1:NsJcKPIW0D0H3NgzPDHmo0WW6SptzPdqg/L1zsIm2hY=
go.opentelemetry.io/otel v1.31.0/go.mod h1:O0C14Yl9FgkjqcCZAsE053C13OaddMYr/hz6clDkEJE=
go.opentelemetry.io/otel/metric v1.31.0 h1:FSErL0ATQAmYHUIzSezZibnyVlft1ybhy4ozRPcF2fE=
go.opentelemetry.io/otel/metric v1.31.0/go.mod h1:C3dEloVbLuYoX41KpmAhOqNriGbA+qqH6PQ5E5mUfnY=
go.opentelemetry.io/otel/sdk v1.31.0 h1:xLY3abVHYZ5HSfOg3l2E5LUj2Cwva5Y7yGxnSW9H5Gk=
go.opentelemetry.io/otel/sdk v1.31.0/go.mod h1:TfRbMdhvxIIr/B2N2LQW2S5v9m3gOQ/08KsbbO5BPT0=
go.opentelemetry.io/otel/sdk/metric v1.31.0 h1:i9hxxLJF/9kkvfHppyLL55aW7iIJz4JjxTeYusH7zMc=
go.opentelemetry.io/otel/sdk/metric v1.31.0/go.mod h1:CRInTMVvNhUKgSAMbKyTMxqOBC0zgyxzW55lZzX43Y8=
go.opentelemetry.io/otel/trace v1.31.0 h1:ffjsj1aRouKewfr85U2aGagJ46+MvodynlQ1HYdmJys=
go.opentelemetry.io/otel/trace v1.31.0/go.mod h1:TXZkRk7SM2ZQLtR6eoAWQFIHPvzQ06FJAsO1tJg480A=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
Expand All @@ -137,12 +151,12 @@ golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241104194629-dd2ea8efbc28 h1:XVhgTWWV3kGQlwJHR3upFWZeTsei6Oks1apkZSeonIE=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241104194629-dd2ea8efbc28/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI=
google.golang.org/grpc v1.68.0 h1:aHQeeJbo8zAkAa3pRzrVjZlbz6uSfeOXlJNQM0RAbz0=
google.golang.org/grpc v1.68.0/go.mod h1:fmSPC5AsjSBCK54MyHRx48kpOti1/jRfOlwEWywNjWA=
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241223144023-3abc09e42ca8 h1:TqExAhdPaB60Ux47Cn0oLV07rGnxZzIsaRhQaqS666A=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241223144023-3abc09e42ca8/go.mod h1:lcTa1sDdWEIHMWlITnIczmw5w60CF9ffkb8Z+DVmmjA=
google.golang.org/grpc v1.69.2 h1:U3S9QEtbXC0bYNvRtcoklF3xGtLViumSYxWykJS+7AU=
google.golang.org/grpc v1.69.2/go.mod h1:vyjdE6jLBI76dgpDojsFGNaHlxdjXN9ghpnd2o7JGZ4=
google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk=
google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
15 changes: 15 additions & 0 deletions opslevel/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package opslevel

import (
"context"
"fmt"

"github.com/opslevel/opslevel-go/v2024"

"github.com/hashicorp/terraform-plugin-framework/diag"
)
Expand All @@ -15,3 +18,15 @@ func read[T any](ctx context.Context, d *diag.Diagnostics, state TerraformSource
d.Append(state.Get(ctx, &data)...)
return data
}

func GetTeamID(d *diag.Diagnostics, client *opslevel.Client, identifier string) *opslevel.ID {
if opslevel.IsID(identifier) {
return opslevel.NewID(identifier)
}
team, err := client.GetTeamWithAlias(identifier)
if err != nil {
d.AddError("opslevel error", fmt.Sprintf("failed to find team with alias '%s': %s", identifier, err))
return opslevel.NewID()
}
return &team.Id
}
8 changes: 3 additions & 5 deletions opslevel/resource_opslevel_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/opslevel/opslevel-go/v2024"
Expand Down Expand Up @@ -86,9 +85,8 @@ func (r *DomainResource) Schema(ctx context.Context, req resource.SchemaRequest,
Optional: true,
},
"owner": schema.StringAttribute{
Description: "The id of the team that owns the domain.",
Description: "The id or alias of the team that owns the domain.",
Optional: true,
Validators: []validator.String{IdStringValidator()},
},
},
}
Expand All @@ -104,7 +102,7 @@ func (r *DomainResource) Create(ctx context.Context, req resource.CreateRequest,
Description: planModel.Description.ValueStringPointer(),
Name: opslevel.RefOf(planModel.Name.ValueString()),
Note: planModel.Note.ValueStringPointer(),
OwnerId: opslevel.NewID(planModel.Owner.ValueString()),
OwnerId: GetTeamID(&resp.Diagnostics, r.client, planModel.Owner.ValueString()),
})
if err != nil {
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to create domain, got error: %s", err))
Expand Down Expand Up @@ -147,7 +145,7 @@ func (r *DomainResource) Update(ctx context.Context, req resource.UpdateRequest,
Description: opslevel.RefOf(planModel.Description.ValueString()),
Name: opslevel.RefOf(planModel.Name.ValueString()),
Note: opslevel.RefOf(planModel.Note.ValueString()),
OwnerId: opslevel.NewID(planModel.Owner.ValueString()),
OwnerId: GetTeamID(&resp.Diagnostics, r.client, planModel.Owner.ValueString()),
})
if err != nil {
resp.Diagnostics.AddError("opslevel client error", fmt.Sprintf("Unable to update domain, got error: %s", err))
Expand Down
13 changes: 9 additions & 4 deletions opslevel/resource_opslevel_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ func (r *SystemResource) Schema(ctx context.Context, req resource.SchemaRequest,
Optional: true,
},
"owner": schema.StringAttribute{
Description: "The id of the team that owns the system.",
Description: "The id or alias of the team that owns the system.",
Optional: true,
Validators: []validator.String{IdStringValidator()},
},
},
}
Expand All @@ -110,9 +109,12 @@ func (r *SystemResource) Create(ctx context.Context, req resource.CreateRequest,
systemInput := opslevel.SystemInput{
Name: planModel.Name.ValueStringPointer(),
Description: planModel.Description.ValueStringPointer(),
OwnerId: opslevel.NewID(planModel.Owner.ValueString()),
OwnerId: GetTeamID(&resp.Diagnostics, r.client, planModel.Owner.ValueString()),
Note: planModel.Note.ValueStringPointer(),
}
if resp.Diagnostics.HasError() {
return
}
if planModel.Domain.ValueString() != "" {
systemInput.Parent = opslevel.NewIdentifier(planModel.Domain.ValueString())
}
Expand Down Expand Up @@ -157,9 +159,12 @@ func (r *SystemResource) Update(ctx context.Context, req resource.UpdateRequest,
systemInput := opslevel.SystemInput{
Name: opslevel.RefOf(planModel.Name.ValueString()),
Description: opslevel.RefOf(planModel.Description.ValueString()),
OwnerId: opslevel.NewID(planModel.Owner.ValueString()),
OwnerId: GetTeamID(&resp.Diagnostics, r.client, planModel.Owner.ValueString()),
Note: opslevel.RefOf(planModel.Note.ValueString()),
}
if resp.Diagnostics.HasError() {
return
}
if planModel.Domain.IsNull() {
systemInput.Parent = opslevel.NewIdentifier()
} else {
Expand Down
Loading