github.com/argoproj/argo-cd/v3@v3.2.1/applicationset/services/plugin/plugin_service.go (about)

     1  package plugin
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	internalhttp "github.com/argoproj/argo-cd/v3/applicationset/services/internal/http"
     9  	"github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
    10  )
    11  
    12  // ServiceRequest is the request object sent to the plugin service.
    13  type ServiceRequest struct {
    14  	// ApplicationSetName is the appSetName of the ApplicationSet for which we're requesting parameters. Useful for logging in
    15  	// the plugin service.
    16  	ApplicationSetName string `json:"applicationSetName"`
    17  	// Input is the map of parameters set in the ApplicationSet spec for this generator.
    18  	Input v1alpha1.PluginInput `json:"input"`
    19  }
    20  
    21  type Output struct {
    22  	// Parameters is the list of parameter sets returned by the plugin.
    23  	Parameters []map[string]any `json:"parameters"`
    24  }
    25  
    26  // ServiceResponse is the response object returned by the plugin service.
    27  type ServiceResponse struct {
    28  	// Output is the map of outputs returned by the plugin.
    29  	Output Output `json:"output"`
    30  }
    31  
    32  type Service struct {
    33  	client     *internalhttp.Client
    34  	appSetName string
    35  }
    36  
    37  func NewPluginService(appSetName string, baseURL string, token string, requestTimeout int) (*Service, error) {
    38  	var clientOptionFns []internalhttp.ClientOptionFunc
    39  
    40  	clientOptionFns = append(clientOptionFns, internalhttp.WithToken(token))
    41  
    42  	if requestTimeout != 0 {
    43  		clientOptionFns = append(clientOptionFns, internalhttp.WithTimeout(requestTimeout))
    44  	}
    45  
    46  	client, err := internalhttp.NewClient(baseURL, clientOptionFns...)
    47  	if err != nil {
    48  		return nil, fmt.Errorf("error creating plugin client: %w", err)
    49  	}
    50  
    51  	return &Service{
    52  		client:     client,
    53  		appSetName: appSetName,
    54  	}, nil
    55  }
    56  
    57  func (p *Service) List(ctx context.Context, parameters v1alpha1.PluginParameters) (*ServiceResponse, error) {
    58  	req, err := p.client.NewRequestWithContext(ctx, http.MethodPost, "api/v1/getparams.execute", ServiceRequest{ApplicationSetName: p.appSetName, Input: v1alpha1.PluginInput{Parameters: parameters}})
    59  	if err != nil {
    60  		return nil, fmt.Errorf("NewRequest returned unexpected error: %w", err)
    61  	}
    62  
    63  	var data ServiceResponse
    64  
    65  	_, err = p.client.Do(req, &data)
    66  	if err != nil {
    67  		return nil, fmt.Errorf("error get api '%s': %w", p.appSetName, err)
    68  	}
    69  
    70  	return &data, err
    71  }