package setting

import (
	"os"

	"github.com/rancher/norman/store/transform"
	"github.com/rancher/norman/types"
	"github.com/rancher/rancher/pkg/settings"
)

func New(store types.Store) types.Store {
	return &transform.Store{
		Store: store,
		Transformer: func(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, opt *types.QueryOptions) (map[string]interface{}, error) {
			v, ok := data["value"]
			value := os.Getenv(settings.GetENVKey(apiContext.ID))
			switch {
			case value != "":
				data["value"] = value
				data["customized"] = false
				data["source"] = "env"

			case !ok || v == "":
				data["value"] = data["default"]
				data["customized"] = false
				data["source"] = "default"
			default:
				data["customized"] = true
				data["source"] = "db"
			}
			return data, nil
		},
	}
}

package setting

import (
	"os"

	"github.com/rancher/norman/store/transform"
	"github.com/rancher/norman/types"
	"github.com/rancher/norman/types/convert"
	"github.com/rancher/rancher/pkg/settings"
)

func New(store types.Store) types.Store {
	return &transform.Store{
		Store: store,
		Transformer: func(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, opt *types.QueryOptions) (map[string]interface{}, error) {
			v, ok := data["value"]
			value := os.Getenv(settings.GetENVKey(convert.ToString(data["id"])))
			switch {
			case value != "":
				data["value"] = value
				data["customized"] = false
				data["source"] = "env"
			case !ok || v == "":
				data["value"] = data["default"]
				data["customized"] = false
				data["source"] = "default"
			default:
				data["customized"] = true
				data["source"] = "db"
			}
			return data, nil
		},
	}
}

Fix setting source attribute is not set when listting settings
Fix setting source attribute is not set when listting settings

**Problem:**
The source attribute is not set when listing settings.
The root cause is that `apiContext.ID` won't be set when calling list
interface.

**Solution:**
Use the `data["id"]` as key to get value from environment variable
