Skip to content

Commit

Permalink
feat: new helper to generate provider block for tf
Browse files Browse the repository at this point in the history
  • Loading branch information
LeCrabe committed Mar 27, 2024
1 parent 0f6a628 commit 6f4d7a6
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
54 changes: 54 additions & 0 deletions pkg/helper/provider_block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package helper

// Provider structur
type Provider struct {
Provider string
Organisation string
}

// New function type that accepts pointer to Provider
// (~= Signature of option functions)
type ProviderOption func(*Provider)

// Provider constructor:
// - desc: Build a new Provider and apply specifics ProviderOption functions
// - args: ProviderOption function
// - return: pointer to Provider
func NewProvider(provider string, opts ...ProviderOption) *Provider {
// default values
const (
defaultOrganisation = ""
)

p := &Provider{
Provider: provider,
Organisation: defaultOrganisation,
}

// ProviderOption functions
for _, opt := range opts {
opt(p)
}

return p
}

// Organisation name:
// - desc: concatenate function that set Provider.Organisation then return Provider
// - args: new organisation name
// - return: pointer to Provider
func (p *Provider) OrganisationName(orgName string) *Provider {
p.Organisation = orgName
return p
}

// Provider block
// - desc: concatenate function that stringify Provider into a terraform block
// - args: none
// - return: string
func (p *Provider) String() string {
s := `provider "` + p.Provider + `" {
organisation = "` + p.Organisation + `"
}`
return s
}
24 changes: 24 additions & 0 deletions pkg/helper/provider_block_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package helper

import "testing"

func TestProvider_String(t *testing.T) {
tests := []struct {
name string
fields *Provider
want string
}{
// TODO: Add test cases.

{name: "test1", fields: NewProvider("clevercloud").OrganisationName("clevercloud"), want: `provider "clevercloud" {
organisation = "clevercloud"
}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.fields.String(); got != tt.want {
t.Errorf("Provider.String() = %v, want %v", got, tt.want)
}
})
}
}
5 changes: 3 additions & 2 deletions pkg/resources/python/resource_python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"go.clever-cloud.com/terraform-provider/pkg"
"go.clever-cloud.com/terraform-provider/pkg/helper"
"go.clever-cloud.com/terraform-provider/pkg/provider/impl"
"go.clever-cloud.com/terraform-provider/pkg/tmp"
"go.clever-cloud.dev/client"
Expand Down Expand Up @@ -68,7 +69,7 @@ func TestAccPython_basic(t *testing.T) {
},
Steps: []resource.TestStep{{
ResourceName: rName,
Config: fmt.Sprintf(providerBlock, org) + fmt.Sprintf(pythonBlock, rName, rName),
Config: helper.NewProvider("clevercloud").OrganisationName(org).String() + fmt.Sprintf(pythonBlock, rName, rName),
Check: resource.ComposeAggregateTestCheckFunc(
// Test the state for provider's populated values
resource.TestMatchResourceAttr(fullName, "id", regexp.MustCompile(`^app_.*$`)),
Expand Down Expand Up @@ -148,7 +149,7 @@ func TestAccPython_basic(t *testing.T) {
),
}, {
ResourceName: rName2,
Config: fmt.Sprintf(providerBlock, org) + fmt.Sprintf(pythonBlock2, rName2, rName2),
Config: helper.NewProvider("clevercloud").OrganisationName(org).String() + fmt.Sprintf(pythonBlock2, rName2, rName2),
Check: func(state *terraform.State) error {
id := state.RootModule().Resources[fullName2].Primary.ID

Expand Down

0 comments on commit 6f4d7a6

Please sign in to comment.