123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- package provider
- import (
- "context"
- "os"
- "github.com/hashicorp-demoapp/hashicups-client-go"
- "github.com/hashicorp/terraform-plugin-framework/datasource"
- "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/types"
- )
- var _ provider.Provider = &hashicupsProvider{}
- func New(version string) func() provider.Provider {
- return func() provider.Provider {
- return &hashicupsProvider{
- version: version,
- }
- }
- }
- type hashicupsProvider struct {
-
-
-
- version string
- }
- func (p *hashicupsProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
- resp.TypeName = "hashicups"
- resp.Version = p.version
- }
- func (p *hashicupsProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
- resp.Schema = schema.Schema{
- Attributes: map[string]schema.Attribute{
- "host": schema.StringAttribute{
- Optional: true,
- },
- "username": schema.StringAttribute{
- Optional: true,
- },
- "password": schema.StringAttribute{
- Optional: true,
- Sensitive: true,
- },
- },
- }
- }
- func (p *hashicupsProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
-
-
- var config hashicupsProviderModel
- diags := req.Config.Get(ctx, &config)
- resp.Diagnostics.Append(diags...)
- if resp.Diagnostics.HasError() {
- return
- }
-
-
-
-
-
- if config.Host.IsUnknown() {
- resp.Diagnostics.AddAttributeError(
- path.Root("host"),
- "Unknown HashiCups API Host",
- "The provider cannot create the HashiCups API client as there is an unknown configuration value for the HashiCups API host. "+
- "Either target apply the source of the value first, set the value statically in the configuration, or use the HASHICUPS_HOST environment variable.",
- )
- }
-
- if config.Username.IsUnknown() {
- resp.Diagnostics.AddAttributeError(
- path.Root("username"),
- "Unknown HashiCups API Username",
- "The provider cannot create the HashiCups API client as there is an unknown configuration value for the HashiCups API username. "+
- "Either target apply the source of the value first, set the value statically in the configuration, or use the HASHICUPS_USERNAME environment variable.",
- )
- }
-
- if config.Password.IsUnknown() {
- resp.Diagnostics.AddAttributeError(
- path.Root("password"),
- "Unknown HashiCups API Password",
- "The provider cannot create the HashiCups API client as there is an unknown configuration value for the HashiCups API password. "+
- "Either target apply the source of the value first, set the value statically in the configuration, or use the HASHICUPS_PASSWORD environment variable.",
- )
- }
-
- if resp.Diagnostics.HasError() {
- return
- }
-
-
-
- host := os.Getenv("HASHICUPS_HOST")
- username := os.Getenv("HASHICUPS_USERNAME")
- password := os.Getenv("HASHICUPS_PASSWORD")
-
- if !config.Host.IsNull() {
- host = config.Host.ValueString()
- }
- if !config.Username.IsNull() {
- username = config.Username.ValueString()
- }
- if !config.Password.IsNull() {
- password = config.Password.ValueString()
- }
-
- if host == "" {
- resp.Diagnostics.AddAttributeError(
- path.Root("host"),
- "Missing HashiCups API Host",
- "The provider cannot create the HashiCups API client as there is a missing or empty value for the HashiCups API host. "+
- "Set the host value in the configuration or use the HASHICUPS_HOST environment variable. "+
- "If either is already set, ensure the value is not empty.",
- )
- }
- if username == "" {
- resp.Diagnostics.AddAttributeError(
- path.Root("username"),
- "Missing HashiCups API Username",
- "The provider cannot create the HashiCups API client as there is a missing or empty value for the HashiCups API username. "+
- "Set the username value in the configuration or use the HASHICUPS_USERNAME environment variable. "+
- "If either is already set, ensure the value is not empty.",
- )
- }
- if password == "" {
- resp.Diagnostics.AddAttributeError(
- path.Root("password"),
- "Missing HashiCups API Password",
- "The provider cannot create the HashiCups API client as there is a missing or empty value for the HashiCups API password. "+
- "Set the password value in the configuration or use the HASHICUPS_PASSWORD environment variable. "+
- "If either is already set, ensure the value is not empty.",
- )
- }
-
- if resp.Diagnostics.HasError() {
- return
- }
-
-
-
-
- client, err := hashicups.NewClient(&host, &username, &password)
- if err != nil {
- resp.Diagnostics.AddError(
- "Unable to Create HashiCups API Client",
- "An unexpected error occurred when creating the HashiCups API client. "+
- "If the error is not clear, please contact the provider developers.\n\n"+
- "HashiCups Client Error: "+err.Error(),
- )
- return
- }
-
-
-
-
-
- resp.DataSourceData = client
- resp.ResourceData = client
- }
- func (p *hashicupsProvider) DataSources(_ context.Context) []func() datasource.DataSource {
- return nil
-
-
-
- }
- func (p *hashicupsProvider) Resources(_ context.Context) []func() resource.Resource {
- return nil
- }
- type hashicupsProviderModel struct {
- Host types.String `tfsdk:"host"`
- Username types.String `tfsdk:"username"`
- Password types.String `tfsdk:"password"`
- }
|