github.com/newrelic/newrelic-client-go@v1.1.0/pkg/synthetics/monitor_scripts.go (about)

     1  package synthetics
     2  
     3  import (
     4  	"context"
     5  	"encoding/base64"
     6  	"fmt"
     7  )
     8  
     9  // Deprecated: Use entities.GetEntitySearch instead.
    10  // GetMonitorScript is used to retrieve the script that belongs
    11  // to a New Relic Synthetics scripted monitor.
    12  func (s *Synthetics) GetMonitorScript(monitorID string) (*MonitorScript, error) {
    13  	return s.GetMonitorScriptWithContext(context.Background(), monitorID)
    14  }
    15  
    16  // Deprecated: Use entities.GetEntitySearchWithContext instead.
    17  // GetMonitorScriptWithContext is used to retrieve the script that belongs
    18  // to a New Relic Synthetics scripted monitor.
    19  func (s *Synthetics) GetMonitorScriptWithContext(ctx context.Context, monitorID string) (*MonitorScript, error) {
    20  	resp := MonitorScript{}
    21  	url := fmt.Sprintf("/v4/monitors/%s/script", monitorID)
    22  	_, err := s.client.GetWithContext(ctx, s.config.Region().SyntheticsURL(url), nil, &resp)
    23  
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  
    28  	decoded, err := base64.StdEncoding.DecodeString(resp.Text)
    29  
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	resp.Text = string(decoded)
    35  
    36  	return &resp, nil
    37  }
    38  
    39  // Deprecated: Use one of following instead:
    40  //    synthetics.SyntheticsUpdateScriptAPIMonitor
    41  //    synthetics.SyntheticsUpdateScriptBrowserMonitor
    42  //
    43  // UpdateMonitorScript is used to add a script to an existing New Relic Synthetics monitor_script.
    44  func (s *Synthetics) UpdateMonitorScript(monitorID string, script MonitorScript) (*MonitorScript, error) {
    45  	return s.UpdateMonitorScriptWithContext(context.Background(), monitorID, script)
    46  }
    47  
    48  // Deprecated: Use one of following instead:
    49  //    synthetics.SyntheticsUpdateScriptAPIMonitorWithContext
    50  //    synthetics.SyntheticsUpdateScriptBrowserMonitorWithContext
    51  //
    52  // UpdateMonitorScriptWithContext is used to add a script to an existing New Relic Synthetics monitor_script.
    53  func (s *Synthetics) UpdateMonitorScriptWithContext(ctx context.Context, monitorID string, script MonitorScript) (*MonitorScript, error) {
    54  	script.Text = base64.StdEncoding.EncodeToString([]byte(script.Text))
    55  
    56  	_, err := s.client.PutWithContext(ctx, s.config.Region().SyntheticsURL("/v4/monitors", monitorID, "/script"), nil, &script, nil)
    57  
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	return &script, nil
    63  }