civo/terraform-provider-civo

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" {
  # ...
}

Is this open?
Could I be assigned to this?? @RealHarshThakur @alessandroargentieri

Hey, Can i take this up?

My approach would be.

  1. Modifying schema in Provider()function in provider.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.",
			},
		},
  1. 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")
	}
  1. 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've created a PR for the same, please take a look #147

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