longview.go raw

   1  package linodego
   2  
   3  import (
   4  	"context"
   5  	"encoding/json"
   6  	"time"
   7  
   8  	"github.com/linode/linodego/internal/parseabletime"
   9  )
  10  
  11  // LongviewClient represents a LongviewClient object
  12  type LongviewClient struct {
  13  	ID          int        `json:"id"`
  14  	APIKey      string     `json:"api_key"`
  15  	Created     *time.Time `json:"-"`
  16  	InstallCode string     `json:"install_code"`
  17  	Label       string     `json:"label"`
  18  	Updated     *time.Time `json:"-"`
  19  	Apps        struct {
  20  		Apache any `json:"apache"`
  21  		MySQL  any `json:"mysql"`
  22  		NginX  any `json:"nginx"`
  23  	} `json:"apps"`
  24  }
  25  
  26  // LongviewClientCreateOptions is an options struct used when Creating a Longview Client
  27  type LongviewClientCreateOptions struct {
  28  	Label string `json:"label"`
  29  }
  30  
  31  // LongviewClientUpdateOptions is an options struct used when Updating a Longview Client
  32  type LongviewClientUpdateOptions struct {
  33  	Label string `json:"label"`
  34  }
  35  
  36  // LongviewPlan represents a Longview Plan object
  37  type LongviewPlan struct {
  38  	ID              string `json:"id"`
  39  	Label           string `json:"label"`
  40  	ClientsIncluded int    `json:"clients_included"`
  41  	Price           struct {
  42  		Hourly  float64 `json:"hourly"`
  43  		Monthly float64 `json:"monthly"`
  44  	} `json:"price"`
  45  }
  46  
  47  // LongviewPlanUpdateOptions is an options struct used when Updating a Longview Plan
  48  type LongviewPlanUpdateOptions struct {
  49  	LongviewSubscription string `json:"longview_subscription"`
  50  }
  51  
  52  // ListLongviewClients lists LongviewClients
  53  func (c *Client) ListLongviewClients(ctx context.Context, opts *ListOptions) ([]LongviewClient, error) {
  54  	return getPaginatedResults[LongviewClient](ctx, c, "longview/clients", opts)
  55  }
  56  
  57  // GetLongviewClient gets the template with the provided ID
  58  func (c *Client) GetLongviewClient(ctx context.Context, clientID int) (*LongviewClient, error) {
  59  	e := formatAPIPath("longview/clients/%d", clientID)
  60  	return doGETRequest[LongviewClient](ctx, c, e)
  61  }
  62  
  63  // CreateLongviewClient creates a Longview Client
  64  func (c *Client) CreateLongviewClient(ctx context.Context, opts LongviewClientCreateOptions) (*LongviewClient, error) {
  65  	return doPOSTRequest[LongviewClient](ctx, c, "longview/clients", opts)
  66  }
  67  
  68  // DeleteLongviewClient deletes a Longview Client
  69  func (c *Client) DeleteLongviewClient(ctx context.Context, clientID int) error {
  70  	e := formatAPIPath("longview/clients/%d", clientID)
  71  	return doDELETERequest(ctx, c, e)
  72  }
  73  
  74  // UpdateLongviewClient updates a Longview Client
  75  func (c *Client) UpdateLongviewClient(ctx context.Context, clientID int, opts LongviewClientUpdateOptions) (*LongviewClient, error) {
  76  	e := formatAPIPath("longview/clients/%d", clientID)
  77  	return doPUTRequest[LongviewClient](ctx, c, e, opts)
  78  }
  79  
  80  // GetLongviewPlan gets the template with the provided ID
  81  func (c *Client) GetLongviewPlan(ctx context.Context) (*LongviewPlan, error) {
  82  	return doGETRequest[LongviewPlan](ctx, c, "longview/plan")
  83  }
  84  
  85  // UpdateLongviewPlan updates a Longview Plan
  86  func (c *Client) UpdateLongviewPlan(ctx context.Context, opts LongviewPlanUpdateOptions) (*LongviewPlan, error) {
  87  	return doPUTRequest[LongviewPlan](ctx, c, "longview/plan", opts)
  88  }
  89  
  90  // UnmarshalJSON implements the json.Unmarshaler interface
  91  func (i *LongviewClient) UnmarshalJSON(b []byte) error {
  92  	type Mask LongviewClient
  93  
  94  	p := struct {
  95  		*Mask
  96  
  97  		Created *parseabletime.ParseableTime `json:"created"`
  98  		Updated *parseabletime.ParseableTime `json:"updated"`
  99  	}{
 100  		Mask: (*Mask)(i),
 101  	}
 102  
 103  	if err := json.Unmarshal(b, &p); err != nil {
 104  		return err
 105  	}
 106  
 107  	i.Created = (*time.Time)(p.Created)
 108  	i.Updated = (*time.Time)(p.Updated)
 109  
 110  	return nil
 111  }
 112