diff --git a/README.md b/README.md index 2d4dffa..cf2e4a2 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,15 @@ provider "towerops" { } ``` +To use a custom API URL (e.g., for self-hosted or development): + +```hcl +provider "towerops" { + token = var.towerops_api_token + api_url = "https://custom.example.com" +} +``` + You can also set the token via environment variable: ```bash diff --git a/docs/index.md b/docs/index.md index 39ac15f..ef2d69d 100644 --- a/docs/index.md +++ b/docs/index.md @@ -45,3 +45,7 @@ resource "towerops_device" "router" { ### Required - `token` (String, Sensitive) - The API token for authenticating with TowerOps. + +### Optional + +- `api_url` (String) - The base URL for the TowerOps API. Defaults to `https://towerops.net`. diff --git a/internal/provider/client.go b/internal/provider/client.go index 963f71b..e439f2c 100644 --- a/internal/provider/client.go +++ b/internal/provider/client.go @@ -19,9 +19,12 @@ type Client struct { } // NewClient creates a new TowerOps API client. -func NewClient(token string) *Client { +func NewClient(token, baseURL string) *Client { + if baseURL == "" { + baseURL = defaultBaseURL + } return &Client{ - BaseURL: defaultBaseURL, + BaseURL: baseURL, Token: token, HTTPClient: &http.Client{ Timeout: 30 * time.Second, diff --git a/internal/provider/provider.go b/internal/provider/provider.go index d858f9f..99ca25c 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -19,7 +19,8 @@ type ToweropsProvider struct { // ToweropsProviderModel describes the provider data model. type ToweropsProviderModel struct { - Token types.String `tfsdk:"token"` + Token types.String `tfsdk:"token"` + APIURL types.String `tfsdk:"api_url"` } // New creates a new provider instance. @@ -45,6 +46,10 @@ func (p *ToweropsProvider) Schema(ctx context.Context, req provider.SchemaReques Required: true, Sensitive: true, }, + "api_url": schema.StringAttribute{ + Description: "The base URL for the TowerOps API. Defaults to https://towerops.net.", + Optional: true, + }, }, } } @@ -73,7 +78,7 @@ func (p *ToweropsProvider) Configure(ctx context.Context, req provider.Configure return } - client := NewClient(config.Token.ValueString()) + client := NewClient(config.Token.ValueString(), config.APIURL.ValueString()) resp.DataSourceData = client resp.ResourceData = client