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