chore(provider): support passing the forgejo api token via an environment variable
Closes #14
This commit is contained in:
parent
6d818fbc0b
commit
f4d69460af
2 changed files with 21 additions and 11 deletions
|
@ -34,5 +34,8 @@ provider "forgejo" {
|
||||||
|
|
||||||
### Required
|
### Required
|
||||||
|
|
||||||
- `api_token` (String, Sensitive) Forgejo's api token
|
- `base_uri` (String) Forgejo's HTTP base URI.
|
||||||
- `base_uri` (String) Forgejo's HTTP base uri
|
|
||||||
|
### Optional
|
||||||
|
|
||||||
|
- `api_token` (String, Sensitive) Forgejo's api token. If not defined, the content of the environment variable `FORGEJO_API_TOKEN` will be used instead.
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
|
|
||||||
"git.adyxax.org/adyxax/terraform-provider-forgejo/internal/client"
|
"git.adyxax.org/adyxax/terraform-provider-forgejo/internal/client"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||||
|
@ -40,12 +41,12 @@ func (p *Provider) Schema(ctx context.Context, req provider.SchemaRequest, resp
|
||||||
resp.Schema = schema.Schema{
|
resp.Schema = schema.Schema{
|
||||||
Attributes: map[string]schema.Attribute{
|
Attributes: map[string]schema.Attribute{
|
||||||
"api_token": schema.StringAttribute{
|
"api_token": schema.StringAttribute{
|
||||||
MarkdownDescription: "Forgejo's api token",
|
MarkdownDescription: "Forgejo's api token. If not defined, the content of the environment variable `FORGEJO_API_TOKEN` will be used instead.",
|
||||||
Required: true,
|
Optional: true,
|
||||||
Sensitive: true,
|
Sensitive: true,
|
||||||
},
|
},
|
||||||
"base_uri": schema.StringAttribute{
|
"base_uri": schema.StringAttribute{
|
||||||
MarkdownDescription: "Forgejo's HTTP base uri",
|
MarkdownDescription: "Forgejo's HTTP base URI.",
|
||||||
Required: true,
|
Required: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -61,14 +62,20 @@ func (p *Provider) Configure(ctx context.Context, req provider.ConfigureRequest,
|
||||||
|
|
||||||
baseURI, err := url.Parse(data.BaseURI.ValueString())
|
baseURI, err := url.Parse(data.BaseURI.ValueString())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
resp.Diagnostics.AddError(
|
resp.Diagnostics.AddError("Invalid forgejo base_uri", fmt.Sprintf("failed to parse base_uri: %s", err))
|
||||||
"Invalid base_uri",
|
|
||||||
fmt.Sprintf(
|
|
||||||
"failed to parse base_uri: %s",
|
|
||||||
err))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
client := client.NewClient(baseURI, data.ApiToken.ValueString())
|
var apiToken string
|
||||||
|
if data.ApiToken.IsNull() {
|
||||||
|
apiToken = os.Getenv("FORGEJO_API_TOKEN")
|
||||||
|
if apiToken == "" {
|
||||||
|
resp.Diagnostics.AddError("Invalid forgejo api_token", "environment variable FORGEJO_API_TOKEN not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
apiToken = data.ApiToken.ValueString()
|
||||||
|
}
|
||||||
|
client := client.NewClient(baseURI, apiToken)
|
||||||
|
|
||||||
resp.DataSourceData = client
|
resp.DataSourceData = client
|
||||||
resp.ResourceData = client
|
resp.ResourceData = client
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue