provider.go (7095B)
1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package provider 5 6 import ( 7 "context" 8 "os" 9 10 "github.com/hashicorp/terraform-plugin-framework/datasource" 11 "github.com/hashicorp/terraform-plugin-framework/path" 12 "github.com/hashicorp/terraform-plugin-framework/provider" 13 "github.com/hashicorp/terraform-plugin-framework/provider/schema" 14 "github.com/hashicorp/terraform-plugin-framework/resource" 15 "github.com/hashicorp/terraform-plugin-framework/types" 16 "github.com/hashicorp/terraform-plugin-log/tflog" 17 "github.com/matrix-org/gomatrix" 18 ) 19 20 // Ensure MatrixProvider satisfies various provider interfaces. 21 var _ provider.Provider = &MatrixProvider{} 22 23 // MatrixProvider defines the provider implementation. 24 type MatrixProvider struct { 25 // version is set to the provider version on release, "dev" when the 26 // provider is built and ran locally, and "test" when running acceptance 27 // testing. 28 version string 29 } 30 31 // MatrixProviderModel describes the provider data model. 32 type MatrixProviderModel struct { 33 ClientServerUrl types.String `tfsdk:"client_server_url"` 34 DefaultAccessToken types.String `tfsdk:"default_access_token"` 35 DefaultUserID types.String `tfsdk:"default_user_id"` 36 } 37 38 func (p *MatrixProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) { 39 resp.TypeName = "matrix" 40 resp.Version = p.version 41 } 42 43 func (p *MatrixProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) { 44 resp.Schema = schema.Schema{ 45 Attributes: map[string]schema.Attribute{ 46 "client_server_url": schema.StringAttribute{ 47 MarkdownDescription: "Address of the matrix server you are acting upon", 48 Required: true, 49 }, 50 "default_access_token": schema.StringAttribute{ 51 MarkdownDescription: "The default access token to use for things like content uploads.", 52 Required: true, 53 Sensitive: true, 54 }, 55 "default_user_id": schema.StringAttribute{ 56 MarkdownDescription: "The default user id to use for things like content uploads. This must match the access_token", 57 Required: true, 58 }, 59 }, 60 } 61 } 62 63 func (p *MatrixProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) { 64 var config MatrixProviderModel 65 66 resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) 67 68 if resp.Diagnostics.HasError() { 69 return 70 } 71 72 if config.ClientServerUrl.IsUnknown() { 73 resp.Diagnostics.AddAttributeError( 74 path.Root("client_server_url"), 75 "Unknown Matrix Server URL", 76 "The provider cannot create the Matrix API client as there is an unknown configuration value for the Matrix API host. "+ 77 "Either target apply the source of the value first, set the value statically in the configuration, or use the MATRIX_CLIENT_SERVER_URL environment variable.", 78 ) 79 } 80 81 if config.DefaultAccessToken.IsUnknown() { 82 resp.Diagnostics.AddAttributeError( 83 path.Root("default_access_token"), 84 "Unknown Default Access Token", 85 "The provider cannot create the Matrix API client as there is an unknown configuration value for the default AccessToken. "+ 86 "Either target apply the source of the value first, set the value statically in the configuration, or use the MATRIX_DEFAULT_ACCESS_TOKEN environment variable.", 87 ) 88 } 89 90 if config.DefaultUserID.IsUnknown() { 91 resp.Diagnostics.AddAttributeError( 92 path.Root("default_user_id"), 93 "Unknown Default User ID", 94 "The provider cannot create the Matrix API client as there is an unknown configuration value for the default UserID. "+ 95 "Either target apply the source of the value first, set the value statically in the configuration, or use the MATRIX_DEFAULT_USERID environment variable.", 96 ) 97 } 98 99 if resp.Diagnostics.HasError() { 100 return 101 } 102 103 // Default values to environment variables, but override 104 // with Terraform configuration value if set. 105 106 client_server_url := os.Getenv("MATRIX_CLIENT_SERVER_URL") 107 default_access_token := os.Getenv("MATRIX_DEFAULT_ACCESS_TOKEN") 108 default_user_id := os.Getenv("MATRIX_DEFAULT_USERID") 109 110 if !config.ClientServerUrl.IsNull() { 111 client_server_url = config.ClientServerUrl.ValueString() 112 } 113 114 if !config.DefaultAccessToken.IsNull() { 115 default_access_token = config.DefaultAccessToken.ValueString() 116 } 117 118 if !config.DefaultUserID.IsNull() { 119 default_user_id = config.DefaultUserID.ValueString() 120 } 121 122 if client_server_url == "" { 123 resp.Diagnostics.AddAttributeError( 124 path.Root("client_server_url"), 125 "Missing Matrix Server URL", 126 "The provider cannot create the Matrix API client as there is a missing or empty value for the Matrix API host. "+ 127 "Set the client_server_url value in the configuration or use the MATRIX_CLIENT_SERVER_URL environment variable. "+ 128 "If either is already set, ensure the value is not empty.", 129 ) 130 } 131 132 if default_access_token == "" { 133 resp.Diagnostics.AddAttributeError( 134 path.Root("default_access_token"), 135 "Missing Default Access Token", 136 "The provider cannot create the Matrix API client as there is a missing or empty value for the default AccessToken. "+ 137 "Set the default_access_token value in the configuration or use the MATRIX_DEFAULT_ACCESS_TOKEN environment variable. "+ 138 "If either is already set, ensure the value is not empty.", 139 ) 140 } 141 142 if default_user_id == "" { 143 resp.Diagnostics.AddAttributeError( 144 path.Root("default_user_id"), 145 "Missing Default UserID", 146 "The provider cannot create the Matrix API client as there is a missing or empty value for the default UserID. "+ 147 "Set the default_user_id value in the configuration or use the MATRIX_DEFAULT_USERID environment variable. "+ 148 "If either is already set, ensure the value is not empty.", 149 ) 150 } 151 152 if resp.Diagnostics.HasError() { 153 return 154 } 155 156 ctx = tflog.SetField(ctx, "client_server_url", client_server_url) 157 ctx = tflog.SetField(ctx, "default_access_token", default_access_token) 158 ctx = tflog.SetField(ctx, "default_user_id", default_user_id) 159 ctx = tflog.MaskFieldValuesWithFieldKeys(ctx, "default_access_token") 160 161 tflog.Debug(ctx, "Creating Matrix client") 162 163 // Example client configuration for data sources and resources 164 client, err := gomatrix.NewClient(client_server_url, default_user_id, default_access_token) 165 if err != nil { 166 resp.Diagnostics.AddError( 167 "Unable to Create Matrix API Client", 168 "An unexpected error occurred when creating the Matrix API client. "+ 169 "If the error is not clear, please contact the provider developers.\n\n"+ 170 "Matrix Client Error: "+err.Error(), 171 ) 172 return 173 } 174 resp.DataSourceData = client 175 resp.ResourceData = client 176 177 tflog.Info(ctx, "Configured Matrix client", map[string]any{"success": true}) 178 } 179 180 func (p *MatrixProvider) Resources(ctx context.Context) []func() resource.Resource { 181 return []func() resource.Resource{} 182 } 183 184 func (p *MatrixProvider) DataSources(ctx context.Context) []func() datasource.DataSource { 185 return []func() datasource.DataSource{} 186 } 187 188 func New(version string) func() provider.Provider { 189 return func() provider.Provider { 190 return &MatrixProvider{ 191 version: version, 192 } 193 } 194 }