-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from pluralsh/sebastian/prod-2580-shared_secre…
…t-terraform-resource feat: add shared secret resource
- Loading branch information
Showing
7 changed files
with
198 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
terraform { | ||
required_providers { | ||
plural = { | ||
source = "pluralsh/plural" | ||
version = "0.2.1" | ||
} | ||
} | ||
} | ||
|
||
provider "plural" { | ||
use_cli = true | ||
} | ||
|
||
data "plural_user" "user" { | ||
email = "[email protected]" | ||
} | ||
|
||
resource "plural_shared_secret" "mysecret" { | ||
name = "mysecret" | ||
secret = "password" | ||
notification_bindings = [ | ||
{ user_id = data.plural_user.user.id } | ||
] | ||
} | ||
|
||
resource "null_resource" "default" { | ||
provisioner "local-exec" { | ||
command = "echo name:${plural_shared_secret.mysecret.name}" | ||
} | ||
} | ||
|
||
output "secretoutput" { | ||
value = plural_shared_secret.mysecret.secret | ||
sensitive = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package model | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
console "github.com/pluralsh/console/go/client" | ||
|
||
"terraform-provider-plural/internal/common" | ||
) | ||
|
||
type SharedSecret struct { | ||
Name types.String `tfsdk:"name"` | ||
Secret types.String `tfsdk:"secret"` | ||
NotificationBindings types.Set `tfsdk:"notification_bindings"` | ||
} | ||
|
||
func (in *SharedSecret) Attributes(ctx context.Context, d diag.Diagnostics) console.SharedSecretAttributes { | ||
return console.SharedSecretAttributes{ | ||
Name: in.Name.ValueString(), | ||
Secret: in.Secret.ValueString(), | ||
NotificationBindings: common.SetToPolicyBindingAttributes(in.NotificationBindings, ctx, d), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package resource | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"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/setplanmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
|
||
"terraform-provider-plural/internal/client" | ||
"terraform-provider-plural/internal/common" | ||
"terraform-provider-plural/internal/model" | ||
) | ||
|
||
var _ resource.ResourceWithConfigure = &sharedSecretResource{} | ||
|
||
func NewSharedSecretResource() resource.Resource { | ||
return &sharedSecretResource{} | ||
} | ||
|
||
type sharedSecretResource struct { | ||
client *client.Client | ||
} | ||
|
||
func (in *sharedSecretResource) Metadata(_ context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) { | ||
response.TypeName = request.ProviderTypeName + "_shared_secret" | ||
} | ||
|
||
func (in *sharedSecretResource) Schema(_ context.Context, _ resource.SchemaRequest, response *resource.SchemaResponse) { | ||
response.Schema = schema.Schema{ | ||
Description: "A one-time-viewable secret shared with a list of eligible users.", | ||
MarkdownDescription: "A one-time-viewable secret shared with a list of eligible users.", | ||
Attributes: map[string]schema.Attribute{ | ||
"name": schema.StringAttribute{ | ||
Description: "The name of this shared secret.", | ||
MarkdownDescription: "The name of this shared secret.", | ||
Required: true, | ||
Validators: []validator.String{ | ||
stringvalidator.LengthAtLeast(1), | ||
}, | ||
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()}, | ||
}, | ||
"secret": schema.StringAttribute{ | ||
Description: "Content of this shared secret.", | ||
MarkdownDescription: "Content of this shared secret.", | ||
Required: true, | ||
Sensitive: true, | ||
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()}, | ||
}, | ||
"notification_bindings": schema.SetNestedAttribute{ | ||
Description: "The users/groups you want this secret to be delivered to.", | ||
MarkdownDescription: "The users/groups you want this secret to be delivered to.", | ||
Optional: true, | ||
NestedObject: schema.NestedAttributeObject{ | ||
Attributes: map[string]schema.Attribute{ | ||
"group_id": schema.StringAttribute{ | ||
Optional: true, | ||
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()}, | ||
}, | ||
"id": schema.StringAttribute{ | ||
Optional: true, | ||
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()}, | ||
}, | ||
"user_id": schema.StringAttribute{ | ||
Optional: true, | ||
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()}, | ||
}, | ||
}, | ||
}, | ||
PlanModifiers: []planmodifier.Set{setplanmodifier.RequiresReplace()}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (in *sharedSecretResource) Configure(_ context.Context, request resource.ConfigureRequest, response *resource.ConfigureResponse) { | ||
if request.ProviderData == nil { | ||
return | ||
} | ||
|
||
data, ok := request.ProviderData.(*common.ProviderData) | ||
if !ok { | ||
response.Diagnostics.AddError( | ||
"Unexpected Project Resource Configure Type", | ||
fmt.Sprintf("Expected *common.ProviderData, got: %T. Please report this issue to the provider developers.", request.ProviderData), | ||
) | ||
return | ||
} | ||
|
||
in.client = data.Client | ||
} | ||
|
||
func (in *sharedSecretResource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) { | ||
data := new(model.SharedSecret) | ||
response.Diagnostics.Append(request.Plan.Get(ctx, data)...) | ||
if response.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
_, err := in.client.ShareSecret(ctx, data.Attributes(ctx, response.Diagnostics)) | ||
if err != nil { | ||
response.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to share a secret, got error: %s", err)) | ||
return | ||
} | ||
|
||
response.Diagnostics.Append(response.State.Set(ctx, &data)...) | ||
} | ||
|
||
func (in *sharedSecretResource) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) { | ||
data := new(model.SharedSecret) | ||
response.Diagnostics.Append(request.State.Get(ctx, data)...) | ||
if response.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
response.Diagnostics.Append(response.State.Set(ctx, data)...) | ||
} | ||
|
||
func (in *sharedSecretResource) Update(_ context.Context, _ resource.UpdateRequest, _ *resource.UpdateResponse) { | ||
// Ignore. | ||
} | ||
|
||
func (in *sharedSecretResource) Delete(_ context.Context, _ resource.DeleteRequest, _ *resource.DeleteResponse) { | ||
// Ignore. | ||
} |