github.com/fastly/go-fastly/v5@v5.3.0/fastly/datadog.go (about) 1 package fastly 2 3 import ( 4 "fmt" 5 "net/url" 6 "sort" 7 "time" 8 ) 9 10 // Datadog represents a Datadog response from the Fastly API. 11 type Datadog struct { 12 ServiceID string `mapstructure:"service_id"` 13 ServiceVersion int `mapstructure:"version"` 14 15 Name string `mapstructure:"name"` 16 Token string `mapstructure:"token"` 17 Region string `mapstructure:"region"` 18 Format string `mapstructure:"format"` 19 FormatVersion uint `mapstructure:"format_version"` 20 ResponseCondition string `mapstructure:"response_condition"` 21 Placement string `mapstructure:"placement"` 22 CreatedAt *time.Time `mapstructure:"created_at"` 23 UpdatedAt *time.Time `mapstructure:"updated_at"` 24 DeletedAt *time.Time `mapstructure:"deleted_at"` 25 } 26 27 // datadogByName is a sortable list of Datadog. 28 type datadogByName []*Datadog 29 30 // Len, Swap, and Less implement the sortable interface. 31 func (s datadogByName) Len() int { return len(s) } 32 func (s datadogByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 33 func (s datadogByName) Less(i, j int) bool { 34 return s[i].Name < s[j].Name 35 } 36 37 // ListDatadogInput is used as input to the ListDatadog function. 38 type ListDatadogInput struct { 39 // ServiceID is the ID of the service (required). 40 ServiceID string 41 42 // ServiceVersion is the specific configuration version (required). 43 ServiceVersion int 44 } 45 46 // ListDatadog returns the list of Datadog for the configuration version. 47 func (c *Client) ListDatadog(i *ListDatadogInput) ([]*Datadog, error) { 48 if i.ServiceID == "" { 49 return nil, ErrMissingServiceID 50 } 51 52 if i.ServiceVersion == 0 { 53 return nil, ErrMissingServiceVersion 54 } 55 56 path := fmt.Sprintf("/service/%s/version/%d/logging/datadog", i.ServiceID, i.ServiceVersion) 57 resp, err := c.Get(path, nil) 58 if err != nil { 59 return nil, err 60 } 61 62 var d []*Datadog 63 if err := decodeBodyMap(resp.Body, &d); err != nil { 64 return nil, err 65 } 66 sort.Stable(datadogByName(d)) 67 return d, nil 68 } 69 70 // CreateDatadogInput is used as input to the CreateDatadog function. 71 type CreateDatadogInput struct { 72 // ServiceID is the ID of the service (required). 73 ServiceID string 74 75 // ServiceVersion is the specific configuration version (required). 76 ServiceVersion int 77 78 Name string `url:"name,omitempty"` 79 Token string `url:"token,omitempty"` 80 Region string `url:"region,omitempty"` 81 Format string `url:"format,omitempty"` 82 FormatVersion uint `url:"format_version,omitempty"` 83 ResponseCondition string `url:"response_condition,omitempty"` 84 Placement string `url:"placement,omitempty"` 85 } 86 87 // CreateDatadog creates a new Datadog logging endpoint on a Fastly service version. 88 func (c *Client) CreateDatadog(i *CreateDatadogInput) (*Datadog, error) { 89 if i.ServiceID == "" { 90 return nil, ErrMissingServiceID 91 } 92 93 if i.ServiceVersion == 0 { 94 return nil, ErrMissingServiceVersion 95 } 96 97 path := fmt.Sprintf("/service/%s/version/%d/logging/datadog", i.ServiceID, i.ServiceVersion) 98 resp, err := c.PostForm(path, i, nil) 99 if err != nil { 100 return nil, err 101 } 102 103 var d *Datadog 104 if err := decodeBodyMap(resp.Body, &d); err != nil { 105 return nil, err 106 } 107 return d, nil 108 } 109 110 // GetDatadogInput is used as input to the GetDatadog function. 111 type GetDatadogInput struct { 112 // ServiceID is the ID of the service (required). 113 ServiceID string 114 115 // ServiceVersion is the specific configuration version (required). 116 ServiceVersion int 117 118 // Name is the name of the Datadog to fetch. 119 Name string 120 } 121 122 // GetDatadog gets the Datadog configuration with the given parameters. 123 func (c *Client) GetDatadog(i *GetDatadogInput) (*Datadog, error) { 124 if i.ServiceID == "" { 125 return nil, ErrMissingServiceID 126 } 127 128 if i.ServiceVersion == 0 { 129 return nil, ErrMissingServiceVersion 130 } 131 132 if i.Name == "" { 133 return nil, ErrMissingName 134 } 135 136 path := fmt.Sprintf("/service/%s/version/%d/logging/datadog/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name)) 137 resp, err := c.Get(path, nil) 138 if err != nil { 139 return nil, err 140 } 141 142 var d *Datadog 143 if err := decodeBodyMap(resp.Body, &d); err != nil { 144 return nil, err 145 } 146 return d, nil 147 } 148 149 // UpdateDatadogInput is used as input to the UpdateDatadog function. 150 type UpdateDatadogInput struct { 151 // ServiceID is the ID of the service (required). 152 ServiceID string 153 154 // ServiceVersion is the specific configuration version (required). 155 ServiceVersion int 156 157 // Name is the name of the Datadog to update. 158 Name string 159 160 NewName *string `url:"name,omitempty"` 161 Token *string `url:"token,omitempty"` 162 Region *string `url:"region,omitempty"` 163 Format *string `url:"format,omitempty"` 164 FormatVersion *uint `url:"format_version,omitempty"` 165 ResponseCondition *string `url:"response_condition,omitempty"` 166 Placement *string `url:"placement,omitempty"` 167 } 168 169 // UpdateDatadog updates a Datadog logging endpoint on a Fastly service version. 170 func (c *Client) UpdateDatadog(i *UpdateDatadogInput) (*Datadog, error) { 171 if i.ServiceID == "" { 172 return nil, ErrMissingServiceID 173 } 174 175 if i.ServiceVersion == 0 { 176 return nil, ErrMissingServiceVersion 177 } 178 179 if i.Name == "" { 180 return nil, ErrMissingName 181 } 182 183 path := fmt.Sprintf("/service/%s/version/%d/logging/datadog/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name)) 184 resp, err := c.PutForm(path, i, nil) 185 if err != nil { 186 return nil, err 187 } 188 189 var d *Datadog 190 if err := decodeBodyMap(resp.Body, &d); err != nil { 191 return nil, err 192 } 193 return d, nil 194 } 195 196 // DeleteDatadogInput is the input parameter to DeleteDatadog. 197 type DeleteDatadogInput struct { 198 // ServiceID is the ID of the service (required). 199 ServiceID string 200 201 // ServiceVersion is the specific configuration version (required). 202 ServiceVersion int 203 204 // Name is the name of the Datadog to delete (required). 205 Name string 206 } 207 208 // DeleteDatadog deletes a Datadog logging endpoint on a Fastly service version. 209 func (c *Client) DeleteDatadog(i *DeleteDatadogInput) error { 210 if i.ServiceID == "" { 211 return ErrMissingServiceID 212 } 213 214 if i.ServiceVersion == 0 { 215 return ErrMissingServiceVersion 216 } 217 218 if i.Name == "" { 219 return ErrMissingName 220 } 221 222 path := fmt.Sprintf("/service/%s/version/%d/logging/datadog/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name)) 223 resp, err := c.Delete(path, nil) 224 if err != nil { 225 return err 226 } 227 228 var r *statusResp 229 if err := decodeBodyMap(resp.Body, &r); err != nil { 230 return err 231 } 232 if !r.Ok() { 233 return ErrStatusNotOk 234 } 235 return nil 236 }