feat(provider): add provider configuration
This commit is contained in:
commit
95834c9ad9
20 changed files with 1167 additions and 0 deletions
68
internal/client/client.go
Normal file
68
internal/client/client.go
Normal file
|
@ -0,0 +1,68 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
baseURI *url.URL
|
||||
headers *http.Header
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
func NewClient(baseURL *url.URL, apiToken string) *Client {
|
||||
return &Client{
|
||||
baseURI: baseURL,
|
||||
headers: &http.Header{
|
||||
"Accept": {"application/json"},
|
||||
"Authorization": {fmt.Sprintf("token %s", apiToken)},
|
||||
"Content-Type": {"application/json"},
|
||||
},
|
||||
httpClient: &http.Client{
|
||||
Timeout: time.Minute,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) Send(ctx context.Context, method string, uriRef *url.URL, payload any, response any) error {
|
||||
uri := c.baseURI.ResolveReference(uriRef)
|
||||
|
||||
var payloadReader io.Reader
|
||||
if payload != nil {
|
||||
if body, err := json.Marshal(payload); err != nil {
|
||||
return fmt.Errorf("cannot marshal payload: %w", err)
|
||||
} else {
|
||||
payloadReader = bytes.NewReader(body)
|
||||
}
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, method, uri.String(), payloadReader)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot create request: %w", err)
|
||||
}
|
||||
req.Header = *c.headers
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot send request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot read response body: %w", err)
|
||||
}
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
return fmt.Errorf("non 2XX status code received: %d, %q", resp.StatusCode, body)
|
||||
}
|
||||
if err = json.Unmarshal(body, response); err != nil {
|
||||
return fmt.Errorf("response body unmarshal failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
83
internal/provider/provider.go
Normal file
83
internal/provider/provider.go
Normal file
|
@ -0,0 +1,83 @@
|
|||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"git.adyxax.org/adyxax/terraform-provider-forgejo/internal/client"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"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"
|
||||
)
|
||||
|
||||
type Provider struct {
|
||||
version string
|
||||
}
|
||||
|
||||
var _ provider.Provider = &Provider{} // Ensure provider defined types fully satisfy framework interfaces.
|
||||
func New(version string) func() provider.Provider {
|
||||
return func() provider.Provider {
|
||||
return &Provider{
|
||||
version: version,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type ProviderModel struct {
|
||||
ApiToken types.String `tfsdk:"api_token"`
|
||||
BaseURI types.String `tfsdk:"base_uri"`
|
||||
}
|
||||
|
||||
func (p *Provider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
|
||||
resp.TypeName = "forgejo"
|
||||
resp.Version = p.version
|
||||
}
|
||||
|
||||
func (p *Provider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"api_token": schema.StringAttribute{
|
||||
MarkdownDescription: "Forgejo's api token",
|
||||
Required: true,
|
||||
Sensitive: true,
|
||||
},
|
||||
"base_uri": schema.StringAttribute{
|
||||
MarkdownDescription: "Forgejo's HTTP base uri",
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Provider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
|
||||
var data ProviderModel
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
baseURI, err := url.Parse(data.BaseURI.ValueString())
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError(
|
||||
"Invalid base_uri",
|
||||
fmt.Sprintf(
|
||||
"failed to parse base_uri: %s",
|
||||
err))
|
||||
return
|
||||
}
|
||||
client := client.NewClient(baseURI, data.ApiToken.ValueString())
|
||||
|
||||
resp.DataSourceData = client
|
||||
resp.ResourceData = client
|
||||
}
|
||||
|
||||
func (p *Provider) Resources(ctx context.Context) []func() resource.Resource {
|
||||
return []func() resource.Resource{}
|
||||
}
|
||||
|
||||
func (p *Provider) DataSources(ctx context.Context) []func() datasource.DataSource {
|
||||
return []func() datasource.DataSource{}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue