Skip to content

Commit

Permalink
Only check host address if not dynamic config
Browse files Browse the repository at this point in the history
  • Loading branch information
rschmied committed Jan 22, 2025
1 parent 9edc5c1 commit d673d8a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 34 deletions.
19 changes: 8 additions & 11 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ on:
permissions:
contents: write

# Default values to simplify job configurations below.
env:
# Go language version to use for building. This value should also be updated
# in the testing workflow if changed.
GO_VERSION: "1.22"

jobs:
goreleaser:
runs-on: ubuntu-latest
Expand All @@ -29,11 +23,14 @@ jobs:
fetch-depth: 0
- uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
# This uses an action (hashicorp/ghaction-import-gpg) that assumes you set your
# private key in the `GPG_PRIVATE_KEY` secret and passphrase in the `PASSPHRASE`
# secret. If you would rather own your own GPG handling, please fork this action
# or use an alternative one for key handling.
go-version-file: "go.mod"
cache: true

# This uses an action (hashicorp/ghaction-import-gpg) that assumes you
# set your private key in the `GPG_PRIVATE_KEY` secret and passphrase
# in the `PASSPHRASE` secret. If you would rather own your own GPG
# handling, please fork this action or use an alternative one for key
# handling.
- name: Import GPG key
id: import_gpg
uses: crazy-max/[email protected]
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

Lists the changes in the provider.

## Version 0.8.3

- only check the CML host address a) if not dynamic provider configuration and
b) when initializing the CML client. This fixes an issue with the cloud-cml
deployment tooling where dynamic provider configuration is used.
- GH release action: take go version from go.mod

## Version 0.8.2

- go version 1.22 used
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/ciscodevnet/terraform-provider-cml2

go 1.22.7

toolchain go1.22.10
go 1.22.10

require (
github.com/google/go-cmp v0.6.0
Expand Down
21 changes: 20 additions & 1 deletion internal/common/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package common
import (
"context"
"fmt"
"net/url"
"sync"

"github.com/ciscodevnet/terraform-provider-cml2/internal/cmlschema"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
cmlclient "github.com/rschmied/gocmlclient"
"github.com/ciscodevnet/terraform-provider-cml2/internal/cmlschema"
)

type ProviderConfig struct {
Expand Down Expand Up @@ -82,6 +83,24 @@ func (r *ProviderConfig) Initialize(ctx context.Context, diag diag.Diagnostics)
"A server address must be configured to use the CML2 provider",
)
}

// address must be https
parsedURL, err := url.Parse(r.data.Address.ValueString())
if err != nil {
diag.AddError(
"Can't parse server address / URL",
err.Error(),
)
}

// Check if the scheme is HTTPS and we have something like a hostname
if parsedURL.Scheme != "https" || len(parsedURL.Host) == 0 {
diag.AddError(
"Invalid server address / URL, ensure it uses HTTPS",
"A valid CML server URL using HTTPS must be provided.",
)
}

if r.data.SkipVerify.IsNull() {
tflog.Warn(ctx, "Unspecified certificate verification, will verify")
r.data.SkipVerify = types.BoolValue(false)
Expand Down
42 changes: 23 additions & 19 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,6 @@ func (p *CML2Provider) Configure(ctx context.Context, req provider.ConfigureRequ
return
}

// address must be https
parsedURL, err := url.Parse(data.Address.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Can't parse server address / URL",
err.Error(),
)
return
}

// Check if the scheme is HTTPS and we have something like a hostname
if parsedURL.Scheme != "https" || len(parsedURL.Host) == 0 {
resp.Diagnostics.AddError(
"Invalid server address / URL, ensure it uses HTTPS",
"The address of the CML server is valid and uses HTTPS as the protocol",
)
return
}

// https://dev.to/camptocamp-ops/how-to-allow-dynamic-terraform-provider-configuration-20ik
dynamic_config := false
if data.DynamicConfig.IsNull() {
Expand All @@ -80,6 +61,29 @@ func (p *CML2Provider) Configure(ctx context.Context, req provider.ConfigureRequ
// )
}

// Only check this for non-dynamic configurations, otherwise the address
// is possibly empty as it can be provided at a later stage
if !dynamic_config {
// address must be https
parsedURL, err := url.Parse(data.Address.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Can't parse server address / URL",
err.Error(),
)
return
}

// Check if the scheme is HTTPS and we have something like a hostname
if parsedURL.Scheme != "https" || len(parsedURL.Host) == 0 {
resp.Diagnostics.AddError(
"Invalid server address / URL, ensure it uses HTTPS",
"A valid CML server URL using HTTPS must be provided.",
)
return
}
}

config := common.NewProviderConfig(&data)
if !dynamic_config {
config.Initialize(ctx, resp.Diagnostics)
Expand Down

0 comments on commit d673d8a

Please sign in to comment.