Add `api_endpoint` to provider setup
zulh-civo opened this issue · 8 comments
In order to use API server other than api.civo.com
, we have to set CIVO_API_URL
environment variable (code is here). It would be nice if we can set it in the TF file itself. Something like what DO has here. It should be optional and always fallback to api.civo.com
as default.
As to the current CIVO_API_URL
environment variable support, I don't have any strong opinion on that. I'm fine to keep it as long as the provider can check for api_endpoint
field and overwrite the CIVO_API_URL
env value.
Something like this would be great:
# Set the variable value in *.tfvars file or using -var="civo_token=..." CLI flag
variable "civo_token" {}
# Specify required provider as maintained by civo
terraform {
required_providers {
civo = {
source = "civo/civo"
}
}
}
# Configure the Civo Provider
provider "civo" {
token = var.civo_token
region = "LON1"
api_endpoint = "https://example.com" <-- this field
}
# Create a web server
resource "civo_instance" "web" {
# ...
}
Something similar needs to be done here: https://github.com/civo/civo-cloud-controller-manager/blob/master/cloud-controller-manager/cmd/civo-cloud-controller-manager/main.go#L26
Is this open?
Could I be assigned to this?? @RealHarshThakur @alessandroargentieri
Hey, Can i take this up?
My approach would be.
- Modifying schema in
Provider()
function inprovider.go
Schema: map[string]*schema.Schema{
"token": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("CIVO_TOKEN", ""),
Description: "This is the Civo API token. Alternatively, this can also be specified using `CIVO_TOKEN` environment variable.",
},
"region": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("CIVO_REGION", ""),
Description: "If region is not set, then no region will be used and them you need expensify in every resource even if you expensify here you can overwrite in a resource.",
},
"api_endpoint": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("CIVO_API_URL", "https://api.civo.com"),
Description: "The Base URL to use for CIVO API.",
},
},
- Adding the below changes in
ProviderConfigure()
function.
if api_url, ok := d.GetOk("api_url"); ok {
apiURL = api_url.(string)
} else {
return nil, fmt.Errorf("SOmething")
}
- Finally returning the client
client, err = civogo.NewClientWithURL(tokenValue, apiURL, regionValue)
Agree with first and third step. In the second step though, we should default to production API URL rather than erroring out.
Agree with first and third step. In the second step though, we should default to production API URL rather than erroring out.
U mean something like this?
if api_url, ok := d.GetOk("api_url"); ok {
apiURL = api_url.(string)
} else {
apiURL, envExists := os.LookupEnv("CIVO_API_URL")
if envExists && apiURL != "" {
client, err = civogo.NewClientWithURL(tokenValue, apiURL, regionValue)
if err != nil {
return nil, err
}
log.Printf("[DEBUG] Civo API URL: %s\n", apiURL)
return client, nil
}
}
I think we shouldn't do it via environment variables and provider config. It should just be the provider config that allows customization of API URL. By default, it should be pointed to https://api.civo.com
, just hardcoded.
I think we shouldn't do it via environment variables and provider config. It should just be the provider config that allows customization of API URL. By default, it should be pointed to
https://api.civo.com
, just hardcoded.
@RealHarshThakur Something like this? 2f73c7b