github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/providers/data/golang/configdata/config.go (about)

     1  // Copyright (c) 2021, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package configdata
     6  
     7  import (
     8  	"context"
     9  
    10  	"github.com/choria-io/go-choria/build"
    11  	"github.com/choria-io/go-choria/config"
    12  	"github.com/choria-io/go-choria/confkey"
    13  	"github.com/choria-io/go-choria/providers/agent/mcorpc/ddl/common"
    14  	"github.com/choria-io/go-choria/providers/data"
    15  	"github.com/choria-io/go-choria/providers/data/ddl"
    16  	"github.com/choria-io/go-choria/providers/data/plugin"
    17  	"github.com/choria-io/go-choria/server/agents"
    18  )
    19  
    20  type ConfigData struct {
    21  	cfg *config.Config
    22  }
    23  
    24  func ChoriaPlugin() *plugin.DataPlugin {
    25  	return plugin.NewDataPlugin("config_item", New)
    26  }
    27  
    28  func New(fw data.Framework) (data.Plugin, error) {
    29  	return &ConfigData{cfg: fw.Configuration()}, nil
    30  }
    31  
    32  func (s *ConfigData) Run(_ context.Context, q data.Query, si agents.ServerInfoSource) (map[string]data.OutputItem, error) {
    33  	item := q.(string)
    34  	val, ok := confkey.InterfaceWithKey(s.cfg, item)
    35  	if !ok {
    36  		val, ok = confkey.InterfaceWithKey(s.cfg.Choria, item)
    37  		if !ok {
    38  			return map[string]data.OutputItem{
    39  				"present": false,
    40  				"value":   nil,
    41  			}, nil
    42  		}
    43  	}
    44  
    45  	return map[string]data.OutputItem{
    46  		"present": true,
    47  		"value":   val,
    48  	}, nil
    49  }
    50  
    51  func (s *ConfigData) DLL() (*ddl.DDL, error) {
    52  	sddl := &ddl.DDL{
    53  		Metadata: ddl.Metadata{
    54  			License:     "Apache-2.0",
    55  			Author:      "R.I.Pienaar <rip@devco.net>",
    56  			Timeout:     1,
    57  			Name:        "config_item",
    58  			Version:     build.Version,
    59  			URL:         "https://choria.io",
    60  			Description: "Runtime value of a configuration items",
    61  			Provider:    "golang",
    62  		},
    63  		Query: &common.InputItem{
    64  			Prompt:      "Configuration Key",
    65  			Description: "A key as found in the configuration file",
    66  			Type:        common.InputTypeString,
    67  			Default:     "",
    68  			Optional:    false,
    69  			Validation:  ".+",
    70  			MaxLength:   256,
    71  		},
    72  		Output: map[string]*common.OutputItem{
    73  			"present": {
    74  				Description: "Indicates if the configuration item is known",
    75  				DisplayAs:   "Present",
    76  				Type:        common.OutputTypeBoolean,
    77  			},
    78  			"value": {
    79  				Description: "The current value of the configuration item, in it's native data type",
    80  				DisplayAs:   "Value",
    81  				Type:        common.OutputTypeAny,
    82  			},
    83  		},
    84  	}
    85  
    86  	return sddl, nil
    87  }