Skip to content

Commit

Permalink
feat(): Allow to set keep_original_source globally
Browse files Browse the repository at this point in the history
  • Loading branch information
Skraye committed Dec 4, 2023
1 parent 55fc12d commit 409bcd4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
18 changes: 10 additions & 8 deletions internal/provider/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,29 @@ import (
const DefaultURL string = "http://localhost:8080"

type Client struct {
HTTPClient *http.Client
Url string
Username *string
Password *string
Jwt *string
ExtraHeader *map[string]string
TenantId *string
HTTPClient *http.Client
Url string
Username *string
Password *string
Jwt *string
ExtraHeader *map[string]string
TenantId *string
KeepOriginalSource *bool
}

type RequestError struct {
StatusCode int
Err error
}

func NewClient(url string, username *string, password *string, jwt *string, extraHeaders *interface{}, tenantId *string) (*Client, error) {
func NewClient(url string, username *string, password *string, jwt *string, extraHeaders *interface{}, tenantId *string, keepOriginalSource *bool) (*Client, error) {
c := Client{
HTTPClient: &http.Client{Timeout: 10 * time.Second},
Url: DefaultURL,
}

c.Url = url
c.KeepOriginalSource = keepOriginalSource

if (username != nil) && (password != nil) {
c.Username = username
Expand Down
9 changes: 8 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ func New(version string, tenant *string) func() *schema.Provider {
Optional: true,
Description: "Extra headers to add to every request",
},
"keep_original_source": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Description: "Keep original source code, keeping comment and indentation.",
DefaultFunc: schema.EnvDefaultFunc("KESTRA_KEEP_ORIGINAL_SOURCE", true),
},
},
DataSourcesMap: map[string]*schema.Resource{
"kestra_binding": dataSourceBinding(),
Expand Down Expand Up @@ -99,6 +105,7 @@ func New(version string, tenant *string) func() *schema.Provider {
password := d.Get("password").(string)
jwt := d.Get("jwt").(string)
extraHeaders := d.Get("extra_headers")
keepOriginalSource := d.Get("keep_original_source").(bool)

tenantId := ""
if tenant != nil {
Expand All @@ -109,7 +116,7 @@ func New(version string, tenant *string) func() *schema.Provider {

var diags diag.Diagnostics

c, err := NewClient(url, &username, &password, &jwt, &extraHeaders, &tenantId)
c, err := NewClient(url, &username, &password, &jwt, &extraHeaders, &tenantId, &keepOriginalSource)
if err != nil {
return nil, diag.FromErr(err)
}
Expand Down
13 changes: 11 additions & 2 deletions internal/provider/resource_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func resourceFlow() *schema.Resource {
Description: "Use the content as source code, keeping comment and indentation.",
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"content": {
Description: "The flow full content in yaml string.",
Expand All @@ -63,8 +62,18 @@ func resourceFlow() *schema.Resource {
func resourceFlowCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*Client)
var diags diag.Diagnostics
var yamlSourceCode bool

// GetOkExists is deprecated but the GetOk does not work correctly with boolean
// Even if you set default to nil for the props, it will always return false if prop is set
localKeepOriginalSource, isLocalSet := d.GetOkExists("keep_original_source")

if isLocalSet == true {
yamlSourceCode = localKeepOriginalSource.(bool)
} else {
yamlSourceCode = *c.KeepOriginalSource
}

yamlSourceCode := d.Get("keep_original_source").(bool)
tenantId := c.TenantId

if yamlSourceCode == true {
Expand Down

0 comments on commit 409bcd4

Please sign in to comment.