Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allows setting the provider host and authorisation_token via CAPELLA prefixed environment variables #239

Merged
merged 2 commits into from
Dec 18, 2024

Conversation

cdsre
Copy link
Contributor

@cdsre cdsre commented Nov 6, 2024

Jira

Description

Please include a summary of the fix/feature/change, including any relevant motivation and context.

Updates the provider to allow setting of the host and authorisation_token as environment variables prefixed with CAPELLA_ and removes authorisation_token as a required attribute in the configuration block as it can now be provided from an environment variable

Type of Change

  • Bug fix (non-breaking change which fixes an issue). Please, add the "bug" label to the PR.
  • New feature (non-breaking change which adds functionality). Please, add the "enhancement" label to the PR.
  • Breaking change (fix or feature that would cause existing functionality to not work as expected). Please, add the "breaking change" label to the PR.
  • This change requires a documentation update
  • Documentation fix/enhancement

Manual Testing Approach

How was this change tested and do you have evidence? (REQUIRED: Select at least 1)

  • Manually tested
  • Unit tested
  • Acceptance tested
  • Unable to test / will not test (Please provide comments in section below)

Testing

Testing

Example terraform code

terraform {
  required_providers {
    couchbase-capella = {
      source = "couchbasecloud/couchbase-capella"
    }
  }
}

data "couchbase-capella_projects" "this" {
  organization_id = "e5456751-XXXXXXXXXXXXXX-aecb859d7db0"
}

output "clusters_list" {
  value = "Number of current projects: ${length(data.couchbase-capella_projects.this.data)}"
}

Execution

$ export CAPELLA_AUTHENTICATION_TOKEN=UTZFcXXXXXXXXXXXXXXXXXXXXXXXGV0WQ==
$ terraform apply --auto-approve
╷
│ Warning: Provider development overrides are in effect
│
│ The following provider development overrides are set in the CLI configuration:
│  - couchbasecloud/couchbase-capella in C:\Users\CDoyle\go\bin
│
│ The behavior may therefore not match any released version of the provider and applying changes may cause the state to become incompatible with published releases.
╵
data.couchbase-capella_projects.this: Reading...
data.couchbase-capella_projects.this: Read complete after 0s

Changes to Outputs:
  + clusters_list = "Number of current projects: 1"

You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

clusters_list = "Number of current projects: 1"

Without using a provider block in the terraform code and setting only the CAPELLA_AUTHORISATION_TOKEN the provider still successfully interacts with the API and retrieves project details.

Required Checklist:

  • I have checked that this change does not generate any credentials and that they are NOT accidentally logged anywhere.
  • I have added tests that prove my fix is effective or that my feature works per HashiCorp requirements
  • I have added any necessary documentation (if required)
  • I have run make fmt and formatted my code
  • I have made sure that no schema field is marked with both requiresReplace and computed

Further comments

The make fmt has formatted the internal/schema/cluster.go There is no functional change here other than the go formatter passing the spaces.

cdsre added 2 commits November 6, 2024 23:08
…PELLA prefixed environment variables

Signed-off-by: Chris Doyle <uk.chris.doyle+cdsre@gmail.com>
Signed-off-by: Chris Doyle <uk.chris.doyle+cdsre@gmail.com>
@cdsre cdsre requested review from a team as code owners November 6, 2024 23:36
@l0n3star l0n3star self-assigned this Dec 16, 2024
@l0n3star
Copy link
Contributor

Also validated the TF_VAR variables work.

Note that I added a debug statement to validate I tested the right build (it prints ###DEBUG### cdsre build) which has been removed.

config:

terraform {
  required_providers {
    couchbase-capella = {
      source = "couchbasecloud/couchbase-capella"
    }
  }
}

provider "couchbase-capella" {}

variable "organization_id" {
  description = "Capella Organization ID"
}

output "organizations_get" {
  value = data.couchbase-capella_organization.existing_organization
}

data "couchbase-capella_organization" "existing_organization" {
  organization_id = var.organization_id
}

run:

 export TF_VAR_host=<host>
 export TF_VAR_auth_token=<token

 terraform plan
╷
│ Warning: Provider development overrides are in effect
│ 
│ The following provider development overrides are set in the CLI configuration:
│  - couchbasecloud/couchbase-capella in /Users/$USER/Documents/github/terraform-provider-couchbase-capella/bin
│ 
│ The behavior may therefore not match any released version of the provider and applying changes may cause the state to become incompatible with published releases.
╵
data.couchbase-capella_organization.existing_organization: Reading...
2024-12-16T09:35:08.789-0800 [WARN]  unexpected data: registry.terraform.io/couchbasecloud/couchbase-capella:stdout="###DEBUG### cdsre build"
data.couchbase-capella_organization.existing_organization: Read complete after 0s [name=cbc-dev]

Changes to Outputs:
  + organizations_get = {
      + audit           = {
          + created_at  = "2020-07-22 12:38:57.437248116 +0000 UTC"
          + created_by  = ""
          + modified_at = "2024-12-12 21:54:56.791985108 +0000 UTC"
          + modified_by = "ffffffff-aaaa-1414-eeee-000000000000"
          + version     = 0
        }
      + description     = ""
      + name            = "cbc-dev"
      + organization_id = "ffffffff-aaaa-1414-eeee-000000000000"
      + preferences     = {
          + session_duration = 7200
        }
    }

You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.

Copy link
Contributor

@l0n3star l0n3star left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution !

@SaicharanCB
Copy link
Contributor

SaicharanCB commented Dec 17, 2024

@l0n3star , @cdsre does this affect or do we need to change anything on the test automation (jenkins) side. We use TF_VAR env variables in jenkins to read the values
From examples it seems like TF_VAR also works and shouldn't be a problem but just want to confirm .

@l0n3star
Copy link
Contributor

l0n3star commented Dec 18, 2024

@SaicharanCB my comment shows TF_VAR still works. so as long the env vars are set correctly and jenkins can get them I think should be fine.

@l0n3star l0n3star merged commit 8c80dba into couchbasecloud:main Dec 18, 2024
@cdsre cdsre deleted the capella-#238 branch December 18, 2024 22:31
@cdsre cdsre restored the capella-#238 branch December 18, 2024 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: Allow provider config to be set from environment variables.
3 participants