Skip to content
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
6 changes: 3 additions & 3 deletions examples/provider/provider.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
provider "onepassword" {
url = "http://localhost:8080"
token = "CONNECT_TOKEN"
connect_url = "http://localhost:8080"
connect_token = "CONNECT_TOKEN"
service_account_token = "SERVICE_ACCOUNT_TOKEN"
account = "ACCOUNT_ID_OR_SIGN_IN_ADDRESS"
account = "ACCOUNT_NAME"
}
2 changes: 1 addition & 1 deletion internal/onepassword/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ func NewClient(ctx context.Context, config ClientConfig) (Client, error) {
ProviderUserAgent: config.ProviderUserAgent,
}), nil
}
return nil, errors.New("Invalid provider configuration. Either Connect credentials (\"token\" and \"url\") or Service Account (\"service_account_token\" or \"account\") credentials should be set.")
return nil, errors.New("Invalid provider configuration. Either Connect credentials (\"connect_token\" and \"connect_url\") or Service Account (\"service_account_token\") or \"account\" should be set.")
}
50 changes: 44 additions & 6 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import (
"fmt"
"os"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/function"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/1Password/terraform-provider-onepassword/v2/internal/onepassword"
Expand All @@ -29,10 +32,13 @@ type OnePasswordProvider struct {

// OnePasswordProviderModel describes the provider data model.
type OnePasswordProviderModel struct {
ConnectHost types.String `tfsdk:"url"`
ConnectToken types.String `tfsdk:"token"`
ConnectHost types.String `tfsdk:"connect_url"`
ConnectToken types.String `tfsdk:"connect_token"`
ServiceAccountToken types.String `tfsdk:"service_account_token"`
Account types.String `tfsdk:"account"`
// Old field names - these are deprecated and will be removed in a future version.
ConnectHostOld types.String `tfsdk:"url"`
ConnectTokenOld types.String `tfsdk:"token"`
}

func (p *OnePasswordProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
Expand All @@ -43,15 +49,40 @@ func (p *OnePasswordProvider) Metadata(ctx context.Context, req provider.Metadat
func (p *OnePasswordProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"url": schema.StringAttribute{
"connect_url": schema.StringAttribute{
MarkdownDescription: "The HTTP(S) URL where your 1Password Connect server can be found. Can also be sourced `OP_CONNECT_HOST` environment variable. Provider will use 1Password Connect server if set.",
Optional: true,
},
"token": schema.StringAttribute{
"connect_token": schema.StringAttribute{
MarkdownDescription: "A valid token for your 1Password Connect server. Can also be sourced from `OP_CONNECT_TOKEN` environment variable. Provider will use 1Password Connect server if set.",
Optional: true,
Sensitive: true,
},
"url": schema.StringAttribute{
MarkdownDescription: "The HTTP(S) URL where your 1Password Connect server can be found. Can also be sourced `OP_CONNECT_HOST` environment variable. Provider will use 1Password Connect server if set. Deprecated: Use `connect_url` instead.",
Optional: true,
Validators: []validator.String{
stringvalidator.ConflictsWith(
path.Expressions{
path.MatchRoot("connect_url"),
}...,
),
},
DeprecationMessage: "The \"url\" field is deprecated and will be removed in a future version. Use \"connect_url\" instead.",
},
"token": schema.StringAttribute{
MarkdownDescription: "A valid token for your 1Password Connect server. Can also be sourced from `OP_CONNECT_TOKEN` environment variable. Provider will use 1Password Connect server if set. Deprecated: Use `connect_token` instead.",
Optional: true,
Sensitive: true,
Validators: []validator.String{
stringvalidator.ConflictsWith(
path.Expressions{
path.MatchRoot("connect_token"),
}...,
),
},
DeprecationMessage: "The \"token\" field is deprecated and will be removed in a future version. Use \"connect_token\" instead.",
},
"service_account_token": schema.StringAttribute{
MarkdownDescription: "A valid 1Password service account token. Can also be sourced from `OP_SERVICE_ACCOUNT_TOKEN` environment variable.",
Optional: true,
Expand Down Expand Up @@ -88,6 +119,15 @@ func (p *OnePasswordProvider) Configure(ctx context.Context, req provider.Config
if !config.ConnectToken.IsNull() {
connectToken = config.ConnectToken.ValueString()
}

// Old field names - these are deprecated and will be removed in a future version.
if !config.ConnectHostOld.IsNull() {
connectHost = config.ConnectHostOld.ValueString()
}
if !config.ConnectTokenOld.IsNull() {
connectToken = config.ConnectTokenOld.ValueString()
}

if !config.ServiceAccountToken.IsNull() {
serviceAccountToken = config.ServiceAccountToken.ValueString()
}
Expand All @@ -100,8 +140,6 @@ func (p *OnePasswordProvider) Configure(ctx context.Context, req provider.Config
// the other one is prompted for, but Terraform then forgets the value for the one that
// is defined in the code. This confusing user-experience can be avoided by handling the
// requirement of one of the attributes manually.
//
// TODO: Investigate if wrapping this as a (framework) validator can be a better fit.
if serviceAccountToken != "" || account != "" {
if connectToken != "" || connectHost != "" {
resp.Diagnostics.AddError("Config conflict", "Either Connect credentials (\"connect_token\" and \"connect_url\") or \"service_account_token\" or \"account\" can be set. Multiple are set. Only one credential must be set.")
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServe
func testAccProviderConfig(url string) string {
return fmt.Sprintf(`
provider "onepassword" {
url = "%s"
token = "<PASSWORD>"
connect_url = "%s"
connect_token = "<PASSWORD>"
}`, url)
}
4 changes: 2 additions & 2 deletions test/e2e/item_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,8 @@ func TestAccItemResourceTags(t *testing.T) {
tags []string
}{
{"CREATE_ITEM_WITH_2_TAGS", []string{"firstTestTag", "secondTestTag"}},
// {"ADD_3RD_TAG", []string{"firstTestTag", "secondTestTag", "thirdTestTag"}},
// {"REMOVE_2_TAGS", []string{"firstTestTag"}},
{"ADD_3RD_TAG", []string{"firstTestTag", "secondTestTag", "thirdTestTag"}},
{"REMOVE_2_TAGS", []string{"firstTestTag"}},
}

testVaultID := vault.GetTestVaultID(t)
Expand Down
Loading