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

Feature: Creating dedicated resource definition for sfx_team #509

Merged
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/splunk-terraform/terraform-provider-signalfx

go 1.21
go 1.23
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annoyingly required to have http.NewServerMux work as intended


toolchain go1.23.2

Expand Down
145 changes: 145 additions & 0 deletions internal/definition/team/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package team

import (
"context"

"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/signalfx/signalfx-go/team"

pmeta "github.com/splunk-terraform/terraform-provider-signalfx/internal/providermeta"
)

const (
ResourceName = "signalfx_team"
AppPath = "/team"
)

func NewResource() *schema.Resource {
return &schema.Resource{
SchemaFunc: newSchema,
SchemaVersion: 1,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
CreateContext: newResourceCreate(),
ReadContext: newResourceRead(),
UpdateContext: newResourceUpdate(),
DeleteContext: newResourceDelete(),
}
}

func newResourceCreate() schema.CreateContextFunc {
return func(ctx context.Context, rd *schema.ResourceData, meta any) diag.Diagnostics {
payload, err := decodeTerraform(rd)
if err != nil {
return diag.FromErr(err)
}
client, err := pmeta.LoadClient(ctx, meta)
if err != nil {
return diag.FromErr(err)
}
tm, err := client.CreateTeam(ctx, &team.CreateUpdateTeamRequest{
Name: payload.Name,
Description: payload.Description,
Members: payload.Members,
NotificationLists: payload.NotificationLists,
})
if err != nil {
return diag.FromErr(err)
}

tflog.Debug(ctx, "Created new team", map[string]any{
"id": tm.Id,
})

u, err := pmeta.LoadApplicationURL(ctx, meta, AppPath, tm.Id)
if err != nil {
return diag.FromErr(err)
}

if err := rd.Set("url", u); err != nil {
return diag.FromErr(err)
}

return diag.FromErr(encodeTerraform(tm, rd))
}
}

func newResourceRead() schema.ReadContextFunc {
return func(ctx context.Context, rd *schema.ResourceData, meta any) diag.Diagnostics {
client, err := pmeta.LoadClient(ctx, meta)
if err != nil {
return diag.FromErr(err)
}

tm, err := client.GetTeam(ctx, rd.Id())
if err != nil {
return diag.FromErr(err)
}
tflog.Debug(ctx, "Succesfully fetched team data")

u, err := pmeta.LoadApplicationURL(ctx, meta, AppPath, tm.Id)
if err != nil {
return diag.FromErr(err)
}

if err := rd.Set("url", u); err != nil {
return diag.FromErr(err)
}

return diag.FromErr(encodeTerraform(tm, rd))
}
}

func newResourceUpdate() schema.UpdateContextFunc {
return func(ctx context.Context, rd *schema.ResourceData, meta any) diag.Diagnostics {
payload, err := decodeTerraform(rd)
if err != nil {
return diag.FromErr(err)
}

client, err := pmeta.LoadClient(ctx, meta)
if err != nil {
return diag.FromErr(err)
}

tm, err := client.UpdateTeam(ctx, rd.Id(), &team.CreateUpdateTeamRequest{
Name: payload.Name,
Description: payload.Description,
Members: payload.Members,
NotificationLists: payload.NotificationLists,
})
if err != nil {
return diag.FromErr(err)
}

u, err := pmeta.LoadApplicationURL(ctx, meta, AppPath, tm.Id)
if err != nil {
return diag.FromErr(err)
}

if err := rd.Set("url", u); err != nil {
return diag.FromErr(err)
}

tflog.Debug(ctx, "Successfully updated team data")

return diag.FromErr(encodeTerraform(tm, rd))
}
}

func newResourceDelete() schema.DeleteContextFunc {
return func(ctx context.Context, rd *schema.ResourceData, meta any) diag.Diagnostics {
client, err := pmeta.LoadClient(ctx, meta)
if err != nil {
return diag.FromErr(err)
}
tflog.Debug(ctx, "Deteting team resource", map[string]any{
"team-id": rd.Id(),
})

return diag.FromErr(client.DeleteTeam(ctx, rd.Id()))
}
}
Loading
Loading