github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/client/choria_provisionclient/action_configure.go (about)

     1  // generated code; DO NOT EDIT
     2  //
     3  // Client for Choria RPC Agent 'choria_provision' Version 0.28.0 generated using Choria version 0.28.0
     4  
     5  package choria_provisionclient
     6  
     7  import (
     8  	"context"
     9  	"encoding/json"
    10  	"fmt"
    11  	"io"
    12  	"sync"
    13  
    14  	"github.com/choria-io/go-choria/protocol"
    15  	rpcclient "github.com/choria-io/go-choria/providers/agent/mcorpc/client"
    16  	"github.com/choria-io/go-choria/providers/agent/mcorpc/ddl/agent"
    17  	"github.com/choria-io/go-choria/providers/agent/mcorpc/replyfmt"
    18  )
    19  
    20  // ConfigureRequester performs a RPC request to choria_provision#configure
    21  type ConfigureRequester struct {
    22  	r    *requester
    23  	outc chan *ConfigureOutput
    24  }
    25  
    26  // ConfigureOutput is the output from the configure action
    27  type ConfigureOutput struct {
    28  	details *ResultDetails
    29  	reply   map[string]any
    30  }
    31  
    32  // ConfigureResult is the result from a configure action
    33  type ConfigureResult struct {
    34  	ddl        *agent.DDL
    35  	stats      *rpcclient.Stats
    36  	outputs    []*ConfigureOutput
    37  	rpcreplies []*replyfmt.RPCReply
    38  	mu         sync.Mutex
    39  }
    40  
    41  func (d *ConfigureResult) RenderResults(w io.Writer, format RenderFormat, displayMode DisplayMode, verbose bool, silent bool, colorize bool, log Log) error {
    42  	d.mu.Lock()
    43  	defer d.mu.Unlock()
    44  
    45  	if d.stats == nil {
    46  		return fmt.Errorf("result stats is not set, result was not completed")
    47  	}
    48  
    49  	results := &replyfmt.RPCResults{
    50  		Agent:   d.stats.Agent(),
    51  		Action:  d.stats.Action(),
    52  		Replies: d.rpcreplies,
    53  		Stats:   d.stats,
    54  	}
    55  
    56  	addl, err := d.ddl.ActionInterface(d.stats.Action())
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	switch format {
    62  	case JSONFormat:
    63  		return results.RenderJSON(w, addl)
    64  	case TableFormat:
    65  		return results.RenderTable(w, addl)
    66  	case TXTFooter:
    67  		results.RenderTXTFooter(w, verbose)
    68  		return nil
    69  	default:
    70  		return results.RenderTXT(w, addl, verbose, silent, replyfmt.DisplayMode(displayMode), colorize, log)
    71  	}
    72  }
    73  
    74  // Stats is the rpc request stats
    75  func (d *ConfigureResult) Stats() Stats {
    76  	return d.stats
    77  }
    78  
    79  // ResultDetails is the details about the request
    80  func (d *ConfigureOutput) ResultDetails() *ResultDetails {
    81  	return d.details
    82  }
    83  
    84  // HashMap is the raw output data
    85  func (d *ConfigureOutput) HashMap() map[string]any {
    86  	return d.reply
    87  }
    88  
    89  // JSON is the JSON representation of the output data
    90  func (d *ConfigureOutput) JSON() ([]byte, error) {
    91  	return json.Marshal(d.reply)
    92  }
    93  
    94  // ParseConfigureOutput parses the result value from the Configure action into target
    95  func (d *ConfigureOutput) ParseConfigureOutput(target any) error {
    96  	j, err := d.JSON()
    97  	if err != nil {
    98  		return fmt.Errorf("could not access payload: %s", err)
    99  	}
   100  
   101  	err = json.Unmarshal(j, target)
   102  	if err != nil {
   103  		return fmt.Errorf("could not unmarshal JSON payload: %s", err)
   104  	}
   105  
   106  	return nil
   107  }
   108  
   109  // Do performs the request
   110  func (d *ConfigureRequester) Do(ctx context.Context) (*ConfigureResult, error) {
   111  	dres := &ConfigureResult{ddl: d.r.client.ddl}
   112  
   113  	addl, err := dres.ddl.ActionInterface(d.r.action)
   114  	if err != nil {
   115  		return nil, err
   116  	}
   117  
   118  	handler := func(pr protocol.Reply, r *rpcclient.RPCReply) {
   119  		// filtered by expr filter
   120  		if r == nil {
   121  			return
   122  		}
   123  
   124  		output := &ConfigureOutput{
   125  			reply: make(map[string]any),
   126  			details: &ResultDetails{
   127  				sender:  pr.SenderID(),
   128  				code:    int(r.Statuscode),
   129  				message: r.Statusmsg,
   130  				ts:      pr.Time(),
   131  			},
   132  		}
   133  
   134  		addl.SetOutputDefaults(output.reply)
   135  
   136  		err := json.Unmarshal(r.Data, &output.reply)
   137  		if err != nil {
   138  			d.r.client.errorf("Could not decode reply from %s: %s", pr.SenderID(), err)
   139  		}
   140  
   141  		// caller wants a channel so we dont return a resultset too (lots of memory etc)
   142  		// this is unused now, no support for setting a channel
   143  		if d.outc != nil {
   144  			d.outc <- output
   145  			return
   146  		}
   147  
   148  		// else prepare our result set
   149  		dres.mu.Lock()
   150  		dres.outputs = append(dres.outputs, output)
   151  		dres.rpcreplies = append(dres.rpcreplies, &replyfmt.RPCReply{
   152  			Sender:   pr.SenderID(),
   153  			RPCReply: r,
   154  		})
   155  		dres.mu.Unlock()
   156  	}
   157  
   158  	res, err := d.r.do(ctx, handler)
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  
   163  	dres.stats = res
   164  
   165  	return dres, nil
   166  }
   167  
   168  // AllOutputs provide access to all outputs
   169  func (d *ConfigureResult) AllOutputs() []*ConfigureOutput {
   170  	return d.outputs
   171  }
   172  
   173  // EachOutput iterates over all results received
   174  func (d *ConfigureResult) EachOutput(h func(r *ConfigureOutput)) {
   175  	for _, resp := range d.outputs {
   176  		h(resp)
   177  	}
   178  }
   179  
   180  // ActionPolicies is an optional input to the configure action
   181  //
   182  // Description: Map of Action Policy documents indexed by file name
   183  func (d *ConfigureRequester) ActionPolicies(v map[string]any) *ConfigureRequester {
   184  	d.r.args["action_policies"] = v
   185  
   186  	return d
   187  }
   188  
   189  // Ca is an optional input to the configure action
   190  //
   191  // Description: PEM text block for the CA
   192  func (d *ConfigureRequester) Ca(v string) *ConfigureRequester {
   193  	d.r.args["ca"] = v
   194  
   195  	return d
   196  }
   197  
   198  // Certificate is an optional input to the configure action
   199  //
   200  // Description: PEM text block for the certificate
   201  func (d *ConfigureRequester) Certificate(v string) *ConfigureRequester {
   202  	d.r.args["certificate"] = v
   203  
   204  	return d
   205  }
   206  
   207  // EcdhPublic is an optional input to the configure action
   208  //
   209  // Description: Required when sending a private key
   210  func (d *ConfigureRequester) EcdhPublic(v string) *ConfigureRequester {
   211  	d.r.args["ecdh_public"] = v
   212  
   213  	return d
   214  }
   215  
   216  // Key is an optional input to the configure action
   217  //
   218  // Description: A RSA private key
   219  func (d *ConfigureRequester) Key(v string) *ConfigureRequester {
   220  	d.r.args["key"] = v
   221  
   222  	return d
   223  }
   224  
   225  // OpaPolicies is an optional input to the configure action
   226  //
   227  // Description: Map of Open Policy Agent Policy documents indexed by file name
   228  func (d *ConfigureRequester) OpaPolicies(v map[string]any) *ConfigureRequester {
   229  	d.r.args["opa_policies"] = v
   230  
   231  	return d
   232  }
   233  
   234  // ServerJwt is an optional input to the configure action
   235  //
   236  // Description: JWT file used to identify the server to the broker for ed25519 based authentication
   237  func (d *ConfigureRequester) ServerJwt(v string) *ConfigureRequester {
   238  	d.r.args["server_jwt"] = v
   239  
   240  	return d
   241  }
   242  
   243  // Ssldir is an optional input to the configure action
   244  //
   245  // Description: Directory for storing the certificate in
   246  func (d *ConfigureRequester) Ssldir(v string) *ConfigureRequester {
   247  	d.r.args["ssldir"] = v
   248  
   249  	return d
   250  }
   251  
   252  // Token is an optional input to the configure action
   253  //
   254  // Description: Authentication token to pass to the server
   255  func (d *ConfigureRequester) Token(v string) *ConfigureRequester {
   256  	d.r.args["token"] = v
   257  
   258  	return d
   259  }
   260  
   261  // Message is the value of the message output
   262  //
   263  // Description: Status message from the Provisioner
   264  func (d *ConfigureOutput) Message() string {
   265  	val := d.reply["message"]
   266  
   267  	return val.(string)
   268  
   269  }