Skip to content

Commit

Permalink
Fix bug where changes were detected even though no change happened
Browse files Browse the repository at this point in the history
  • Loading branch information
karo-moia committed Jan 9, 2023
1 parent 6f96baa commit a7ab509
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
15 changes: 14 additions & 1 deletion opensearch/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ func Provider() *schema.Provider {
DefaultFunc: schema.EnvDefaultFunc("OS_BASE_URL", nil),
Description: "The Opensearch base url",
},
"sync_index_pattern_fields": {
Type: schema.TypeBool,
Optional: true,
Sensitive: false,
Default: false,
Description: "Usually in index-patterns the fields are automatically generated from the matched indices. " +
"If you instead explicitly want to track index-pattern-fields with terraform, set this value to true.",
},
},
ResourcesMap: map[string]*schema.Resource{
"opensearch_saved_object": resourceSavedObjects(),
Expand Down Expand Up @@ -97,8 +105,13 @@ func providerConfigure(_ context.Context, d *schema.ResourceData) (any, diag.Dia

cfg.RoundTripper = signer

var syncIndexPatternFields bool
if v, ok := d.GetOk("sync_index_pattern_fields"); ok {
syncIndexPatternFields = v.(bool)
}

// init providers
savedObjectsProvider := saved_objects.NewSavedObjectsProvider(cfg.BaseUrl, &http.Client{Transport: cfg.RoundTripper})
savedObjectsProvider := saved_objects.NewSavedObjectsProvider(cfg.BaseUrl, &http.Client{Transport: cfg.RoundTripper}, syncIndexPatternFields)

// pass providers to the client
client := &OpensearchDashboardsClient{
Expand Down
18 changes: 13 additions & 5 deletions pkg/saved_objects/saved_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
)

const indexPatternType = "index-pattern"

type SavedObjectsProvider struct {
BaseUrl string
httpClient *http.Client
BaseUrl string
httpClient *http.Client
SyncIndexPatternFields bool
}

func NewSavedObjectsProvider(baseUrl string, client *http.Client) *SavedObjectsProvider {
func NewSavedObjectsProvider(baseUrl string, client *http.Client, syncIndexPatternFields bool) *SavedObjectsProvider {
return &SavedObjectsProvider{
BaseUrl: baseUrl,
httpClient: client,
BaseUrl: baseUrl,
httpClient: client,
SyncIndexPatternFields: syncIndexPatternFields,
}
}

Expand Down Expand Up @@ -72,6 +76,10 @@ func (p *SavedObjectsProvider) GetObject(ctx context.Context, obj *SavedObjectOS
return nil, diag.FromErr(fmt.Errorf("request failed, cannot decode response body, err %w ", err))
}

if obj.Type == indexPatternType && !p.SyncIndexPatternFields {
delete(obj.Attributes, "fields")
}

stringifiedAttributes, err := json.Marshal(obj.Attributes)
if err != nil {
return nil, diag.FromErr(fmt.Errorf("request failed, cannot stringify attibutes from response body, err %w ", err))
Expand Down

0 comments on commit a7ab509

Please sign in to comment.