-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: switched to seperate tag resource and datasource model
- Loading branch information
Showing
8 changed files
with
574 additions
and
75 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,24 @@ | ||
terraform { | ||
required_providers { | ||
infisical = { | ||
# version = <latest version> | ||
source = "infisical/infisical" | ||
} | ||
} | ||
} | ||
|
||
provider "infisical" { | ||
host = "https://app.infisical.com" # Only required if using self hosted instance of Infisical, default is https://app.infisical.com | ||
client_id = "<>" | ||
client_secret = "<>" | ||
} | ||
|
||
data "infisical_secret_tag" "terraform" { | ||
slug = "terraform" | ||
project_id = "PROJECT_ID" | ||
} | ||
|
||
output "secret-tag" { | ||
value = data.infisical_secret_tag.terraform | ||
} | ||
|
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,131 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package datasource | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
infisical "terraform-provider-infisical/internal/client" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
// Ensure provider defined types fully satisfy framework interfaces. | ||
var _ datasource.DataSource = &SecretTagsDataSource{} | ||
|
||
func NewSecretTagDataSource() datasource.DataSource { | ||
return &SecretTagsDataSource{} | ||
} | ||
|
||
// SecretDataSource defines the data source implementation. | ||
type SecretTagsDataSource struct { | ||
client *infisical.Client | ||
} | ||
|
||
// ExampleDataSourceModel describes the data source data model. | ||
type SecretTagDataSourceModel struct { | ||
ID types.String `tfsdk:"id"` | ||
ProjectID types.String `tfsdk:"project_id"` | ||
Name types.String `tfsdk:"name"` | ||
Slug types.String `tfsdk:"slug"` | ||
Color types.String `tfsdk:"color"` | ||
} | ||
|
||
func (d *SecretTagsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_secret_tag" | ||
} | ||
|
||
func (d *SecretTagsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Description: "Interact with Infisical secretTag secret tag.", | ||
Attributes: map[string]schema.Attribute{ | ||
"slug": schema.StringAttribute{ | ||
Description: "The slug of the tag to fetch", | ||
Required: true, | ||
}, | ||
"project_id": schema.StringAttribute{ | ||
Description: "The secretTag ID of the tag to fetch", | ||
Required: true, | ||
}, | ||
|
||
"id": schema.StringAttribute{ | ||
Description: "The ID of the secret tag", | ||
Computed: true, | ||
}, | ||
"name": schema.StringAttribute{ | ||
Description: "The name of the secret tag", | ||
Computed: true, | ||
}, | ||
"color": schema.StringAttribute{ | ||
Description: "The color of the secret tag", | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (d *SecretTagsDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
// Prevent panic if the provider has not been configured. | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
client, ok := req.ProviderData.(*infisical.Client) | ||
|
||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Data Source Configure Type", | ||
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), | ||
) | ||
|
||
return | ||
} | ||
|
||
d.client = client | ||
} | ||
|
||
func (d *SecretTagsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
|
||
if d.client.Config.AuthStrategy != infisical.AuthStrategy.UNIVERSAL_MACHINE_IDENTITY { | ||
resp.Diagnostics.AddError( | ||
"Unable to create secretTag tag", | ||
"Only Machine Identity authentication is supported for this operation", | ||
) | ||
return | ||
} | ||
|
||
var data SecretTagDataSourceModel | ||
|
||
// Read Terraform configuration data into the model | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
secretTag, err := d.client.GetProjectTagBySlug(infisical.GetProjectTagBySlugRequest{ | ||
TagSlug: data.Slug.ValueString(), | ||
ProjectID: data.ProjectID.ValueString(), | ||
}) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
"Something went wrong while fetching the secret tag", | ||
"If the error is not clear, please get in touch at infisical.com/slack\n\n"+ | ||
"Infisical Client Error: "+err.Error(), | ||
) | ||
} | ||
|
||
data = SecretTagDataSourceModel{ | ||
ID: types.StringValue(secretTag.Tag.ID), | ||
Name: types.StringValue(secretTag.Tag.Name), | ||
Slug: types.StringValue(secretTag.Tag.Slug), | ||
ProjectID: data.ProjectID, | ||
} | ||
|
||
// Save data into Terraform state | ||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} |
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
Oops, something went wrong.