-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new helper to generate provider block for tf
- Loading branch information
LeCrabe
committed
Mar 27, 2024
1 parent
0f6a628
commit 6f4d7a6
Showing
3 changed files
with
81 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters