11package agent
22
33import (
4- "bytes"
54 "context"
65 "encoding/json"
76 "errors"
@@ -425,7 +424,8 @@ func postData(ctx context.Context, config CombinedConfig, preflightClient client
425424
426425 log .V (logs .Debug ).Info ("Posting data" , "baseURL" , baseURL )
427426
428- if config .TLSPKMode == VenafiCloudKeypair || config .TLSPKMode == VenafiCloudVenafiConnection {
427+ switch config .TLSPKMode { // nolint:exhaustive
428+ case VenafiCloudKeypair , VenafiCloudVenafiConnection :
429429 // orgID and clusterID are not required for Venafi Cloud auth
430430 err := preflightClient .PostDataReadingsWithOptions (ctx , readings , client.Options {
431431 ClusterName : config .ClusterID ,
@@ -437,55 +437,22 @@ func postData(ctx context.Context, config CombinedConfig, preflightClient client
437437 log .Info ("Data sent successfully" )
438438
439439 return nil
440- }
441-
442- if config .OrganizationID == "" {
443- data , err := json .Marshal (readings )
444- if err != nil {
445- return fmt .Errorf ("Cannot marshal readings: %+v" , err )
446- }
447-
448- // log and collect metrics about the upload size
449- metric := metricPayloadSize .With (
450- prometheus.Labels {"organization" : config .OrganizationID , "cluster" : config .ClusterID },
451- )
452- metric .Set (float64 (len (data )))
453- log .Info ("Data readings" , "uploadSize" , len (data ))
454- path := config .EndpointPath
455- if path == "" {
456- path = "/api/v1/datareadings"
457- }
458- res , err := preflightClient .Post (ctx , path , bytes .NewBuffer (data ))
459440
441+ case JetstackSecureOAuth , JetstackSecureAPIToken :
442+ err := preflightClient .PostDataReadingsWithOptions (ctx , readings , client.Options {
443+ OrgID : config .OrganizationID ,
444+ ClusterID : config .ClusterID ,
445+ })
460446 if err != nil {
461- return fmt .Errorf ("failed to post data: %+v" , err )
462- }
463- if code := res .StatusCode ; code < 200 || code >= 300 {
464- errorContent := ""
465- body , _ := io .ReadAll (res .Body )
466- if err == nil {
467- errorContent = string (body )
468- }
469- defer res .Body .Close ()
470-
471- return fmt .Errorf ("received response with status code %d. Body: [%s]" , code , errorContent )
447+ return fmt .Errorf ("post to server failed: %+v" , err )
472448 }
473449 log .Info ("Data sent successfully" )
474450
475451 return err
476- }
477452
478- if config . ClusterID == "" {
479- return fmt .Errorf ("post to server failed: missing clusterID from agent configuration" )
453+ default :
454+ return fmt .Errorf ("not implemented for mode %s" , config . TLSPKMode )
480455 }
481-
482- err := preflightClient .PostDataReadings (ctx , config .OrganizationID , config .ClusterID , readings )
483- if err != nil {
484- return fmt .Errorf ("post to server failed: %+v" , err )
485- }
486- log .Info ("Data sent successfully" )
487-
488- return nil
489456}
490457
491458// listenAndServe starts the supplied HTTP server and stops it gracefully when
0 commit comments