github.com/fastly/go-fastly/v6@v6.8.0/fastly/s3.go (about)

     1  package fastly
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"sort"
     7  	"time"
     8  )
     9  
    10  type S3Redundancy string
    11  
    12  func S3RedundancyPtr(v S3Redundancy) *S3Redundancy { return &v }
    13  
    14  type S3ServerSideEncryption string
    15  
    16  func S3ServerSideEncryptionPtr(v S3ServerSideEncryption) *S3ServerSideEncryption { return &v }
    17  
    18  type S3AccessControlList string
    19  
    20  func S3AccessControlListPtr(v S3AccessControlList) *S3AccessControlList { return &v }
    21  
    22  const (
    23  	S3RedundancyStandard                 S3Redundancy = "standard"
    24  	S3RedundancyIntelligentTiering       S3Redundancy = "intelligent_tiering"
    25  	S3RedundancyStandardIA               S3Redundancy = "standard_ia"
    26  	S3RedundancyOneZoneIA                S3Redundancy = "onezone_ia"
    27  	S3RedundancyGlacierInstantRetrieval  S3Redundancy = "glacier_ir"
    28  	S3RedundancyGlacierFlexibleRetrieval S3Redundancy = "glacier"
    29  	S3RedundancyGlacierDeepArchive       S3Redundancy = "deep_archive"
    30  	S3RedundancyReduced                  S3Redundancy = "reduced_redundancy"
    31  
    32  	S3ServerSideEncryptionAES S3ServerSideEncryption = "AES256"
    33  	S3ServerSideEncryptionKMS S3ServerSideEncryption = "aws:kms"
    34  
    35  	S3AccessControlListPrivate                S3AccessControlList = "private"
    36  	S3AccessControlListPublicRead             S3AccessControlList = "public-read"
    37  	S3AccessControlListPublicReadWrite        S3AccessControlList = "public-read-write"
    38  	S3AccessControlListAWSExecRead            S3AccessControlList = "aws-exec-read"
    39  	S3AccessControlListAuthenticatedRead      S3AccessControlList = "authenticated-read"
    40  	S3AccessControlListBucketOwnerRead        S3AccessControlList = "bucket-owner-read"
    41  	S3AccessControlListBucketOwnerFullControl S3AccessControlList = "bucket-owner-full-control"
    42  )
    43  
    44  // S3 represents a S3 response from the Fastly API.
    45  type S3 struct {
    46  	ServiceID      string `mapstructure:"service_id"`
    47  	ServiceVersion int    `mapstructure:"version"`
    48  
    49  	Name                         string                 `mapstructure:"name"`
    50  	BucketName                   string                 `mapstructure:"bucket_name"`
    51  	Domain                       string                 `mapstructure:"domain"`
    52  	AccessKey                    string                 `mapstructure:"access_key"`
    53  	SecretKey                    string                 `mapstructure:"secret_key"`
    54  	IAMRole                      string                 `mapstructure:"iam_role"`
    55  	Path                         string                 `mapstructure:"path"`
    56  	Period                       uint                   `mapstructure:"period"`
    57  	CompressionCodec             string                 `mapstructure:"compression_codec"`
    58  	GzipLevel                    uint8                  `mapstructure:"gzip_level"`
    59  	Format                       string                 `mapstructure:"format"`
    60  	FormatVersion                uint                   `mapstructure:"format_version"`
    61  	ResponseCondition            string                 `mapstructure:"response_condition"`
    62  	MessageType                  string                 `mapstructure:"message_type"`
    63  	TimestampFormat              string                 `mapstructure:"timestamp_format"`
    64  	Placement                    string                 `mapstructure:"placement"`
    65  	PublicKey                    string                 `mapstructure:"public_key"`
    66  	Redundancy                   S3Redundancy           `mapstructure:"redundancy"`
    67  	ServerSideEncryptionKMSKeyID string                 `mapstructure:"server_side_encryption_kms_key_id"`
    68  	ServerSideEncryption         S3ServerSideEncryption `mapstructure:"server_side_encryption"`
    69  	CreatedAt                    *time.Time             `mapstructure:"created_at"`
    70  	UpdatedAt                    *time.Time             `mapstructure:"updated_at"`
    71  	DeletedAt                    *time.Time             `mapstructure:"deleted_at"`
    72  	ACL                          S3AccessControlList    `mapstructure:"acl"`
    73  }
    74  
    75  // s3sByName is a sortable list of S3s.
    76  type s3sByName []*S3
    77  
    78  // Len, Swap, and Less implement the sortable interface.
    79  func (s s3sByName) Len() int      { return len(s) }
    80  func (s s3sByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
    81  func (s s3sByName) Less(i, j int) bool {
    82  	return s[i].Name < s[j].Name
    83  }
    84  
    85  // ListS3sInput is used as input to the ListS3s function.
    86  type ListS3sInput struct {
    87  	// ServiceID is the ID of the service (required).
    88  	ServiceID string
    89  
    90  	// ServiceVersion is the specific configuration version (required).
    91  	ServiceVersion int
    92  }
    93  
    94  // ListS3s returns the list of S3s for the configuration version.
    95  func (c *Client) ListS3s(i *ListS3sInput) ([]*S3, error) {
    96  	if i.ServiceID == "" {
    97  		return nil, ErrMissingServiceID
    98  	}
    99  
   100  	if i.ServiceVersion == 0 {
   101  		return nil, ErrMissingServiceVersion
   102  	}
   103  
   104  	path := fmt.Sprintf("/service/%s/version/%d/logging/s3", i.ServiceID, i.ServiceVersion)
   105  	resp, err := c.Get(path, nil)
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  	defer resp.Body.Close()
   110  
   111  	var s3s []*S3
   112  	if err := decodeBodyMap(resp.Body, &s3s); err != nil {
   113  		return nil, err
   114  	}
   115  	sort.Stable(s3sByName(s3s))
   116  	return s3s, nil
   117  }
   118  
   119  // CreateS3Input is used as input to the CreateS3 function.
   120  type CreateS3Input struct {
   121  	// ServiceID is the ID of the service (required).
   122  	ServiceID string
   123  
   124  	// ServiceVersion is the specific configuration version (required).
   125  	ServiceVersion int
   126  
   127  	Name                         string                 `url:"name,omitempty"`
   128  	BucketName                   string                 `url:"bucket_name,omitempty"`
   129  	Domain                       string                 `url:"domain,omitempty"`
   130  	AccessKey                    string                 `url:"access_key,omitempty"`
   131  	SecretKey                    string                 `url:"secret_key,omitempty"`
   132  	IAMRole                      string                 `url:"iam_role,omitempty"`
   133  	Path                         string                 `url:"path,omitempty"`
   134  	Period                       uint                   `url:"period,omitempty"`
   135  	CompressionCodec             string                 `url:"compression_codec,omitempty"`
   136  	GzipLevel                    uint8                  `url:"gzip_level,omitempty"`
   137  	Format                       string                 `url:"format,omitempty"`
   138  	MessageType                  string                 `url:"message_type,omitempty"`
   139  	FormatVersion                uint                   `url:"format_version,omitempty"`
   140  	ResponseCondition            string                 `url:"response_condition,omitempty"`
   141  	TimestampFormat              string                 `url:"timestamp_format,omitempty"`
   142  	Redundancy                   S3Redundancy           `url:"redundancy,omitempty"`
   143  	Placement                    string                 `url:"placement,omitempty"`
   144  	PublicKey                    string                 `url:"public_key,omitempty"`
   145  	ServerSideEncryptionKMSKeyID string                 `url:"server_side_encryption_kms_key_id,omitempty"`
   146  	ServerSideEncryption         S3ServerSideEncryption `url:"server_side_encryption,omitempty"`
   147  	ACL                          S3AccessControlList    `url:"acl,omitempty"`
   148  }
   149  
   150  // CreateS3 creates a new Fastly S3.
   151  func (c *Client) CreateS3(i *CreateS3Input) (*S3, error) {
   152  	if i.ServiceID == "" {
   153  		return nil, ErrMissingServiceID
   154  	}
   155  
   156  	if i.ServiceVersion == 0 {
   157  		return nil, ErrMissingServiceVersion
   158  	}
   159  
   160  	if i.ServerSideEncryption == S3ServerSideEncryptionKMS && i.ServerSideEncryptionKMSKeyID == "" {
   161  		return nil, ErrMissingServerSideEncryptionKMSKeyID
   162  	}
   163  
   164  	path := fmt.Sprintf("/service/%s/version/%d/logging/s3", i.ServiceID, i.ServiceVersion)
   165  	resp, err := c.PostForm(path, i, nil)
   166  	if err != nil {
   167  		return nil, err
   168  	}
   169  	defer resp.Body.Close()
   170  
   171  	var s3 *S3
   172  	if err := decodeBodyMap(resp.Body, &s3); err != nil {
   173  		return nil, err
   174  	}
   175  	return s3, nil
   176  }
   177  
   178  // GetS3Input is used as input to the GetS3 function.
   179  type GetS3Input struct {
   180  	// ServiceID is the ID of the service (required).
   181  	ServiceID string
   182  
   183  	// ServiceVersion is the specific configuration version (required).
   184  	ServiceVersion int
   185  
   186  	// Name is the name of the S3 to fetch.
   187  	Name string
   188  }
   189  
   190  // GetS3 gets the S3 configuration with the given parameters.
   191  func (c *Client) GetS3(i *GetS3Input) (*S3, 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/s3/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name))
   205  	resp, err := c.Get(path, nil)
   206  	if err != nil {
   207  		return nil, err
   208  	}
   209  	defer resp.Body.Close()
   210  
   211  	var s3 *S3
   212  	if err := decodeBodyMap(resp.Body, &s3); err != nil {
   213  		return nil, err
   214  	}
   215  	return s3, nil
   216  }
   217  
   218  // UpdateS3Input is used as input to the UpdateS3 function.
   219  type UpdateS3Input struct {
   220  	// ServiceID is the ID of the service (required).
   221  	ServiceID string
   222  
   223  	// ServiceVersion is the specific configuration version (required).
   224  	ServiceVersion int
   225  
   226  	// Name is the name of the S3 to update.
   227  	Name string
   228  
   229  	NewName                      *string                 `url:"name,omitempty"`
   230  	BucketName                   *string                 `url:"bucket_name,omitempty"`
   231  	Domain                       *string                 `url:"domain,omitempty"`
   232  	AccessKey                    *string                 `url:"access_key,omitempty"`
   233  	SecretKey                    *string                 `url:"secret_key,omitempty"`
   234  	IAMRole                      *string                 `url:"iam_role,omitempty"`
   235  	Path                         *string                 `url:"path,omitempty"`
   236  	Period                       *uint                   `url:"period,omitempty"`
   237  	CompressionCodec             *string                 `url:"compression_codec,omitempty"`
   238  	GzipLevel                    *uint8                  `url:"gzip_level,omitempty"`
   239  	Format                       *string                 `url:"format,omitempty"`
   240  	FormatVersion                *uint                   `url:"format_version,omitempty"`
   241  	ResponseCondition            *string                 `url:"response_condition,omitempty"`
   242  	MessageType                  *string                 `url:"message_type,omitempty"`
   243  	TimestampFormat              *string                 `url:"timestamp_format,omitempty"`
   244  	Redundancy                   *S3Redundancy           `url:"redundancy,omitempty"`
   245  	Placement                    *string                 `url:"placement,omitempty"`
   246  	PublicKey                    *string                 `url:"public_key,omitempty"`
   247  	ServerSideEncryptionKMSKeyID *string                 `url:"server_side_encryption_kms_key_id,omitempty"`
   248  	ServerSideEncryption         *S3ServerSideEncryption `url:"server_side_encryption,omitempty"`
   249  	ACL                          *S3AccessControlList    `url:"acl,omitempty"`
   250  }
   251  
   252  // UpdateS3 updates a specific S3.
   253  func (c *Client) UpdateS3(i *UpdateS3Input) (*S3, error) {
   254  	if i.ServiceID == "" {
   255  		return nil, ErrMissingServiceID
   256  	}
   257  
   258  	if i.ServiceVersion == 0 {
   259  		return nil, ErrMissingServiceVersion
   260  	}
   261  
   262  	if i.Name == "" {
   263  		return nil, ErrMissingName
   264  	}
   265  
   266  	if i.ServerSideEncryption != nil && *i.ServerSideEncryption == S3ServerSideEncryptionKMS && *i.ServerSideEncryptionKMSKeyID == "" {
   267  		return nil, ErrMissingServerSideEncryptionKMSKeyID
   268  	}
   269  
   270  	path := fmt.Sprintf("/service/%s/version/%d/logging/s3/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name))
   271  	resp, err := c.PutForm(path, i, nil)
   272  	if err != nil {
   273  		return nil, err
   274  	}
   275  	defer resp.Body.Close()
   276  
   277  	var s3 *S3
   278  	if err := decodeBodyMap(resp.Body, &s3); err != nil {
   279  		return nil, err
   280  	}
   281  	return s3, nil
   282  }
   283  
   284  // DeleteS3Input is the input parameter to DeleteS3.
   285  type DeleteS3Input struct {
   286  	// ServiceID is the ID of the service (required).
   287  	ServiceID string
   288  
   289  	// ServiceVersion is the specific configuration version (required).
   290  	ServiceVersion int
   291  
   292  	// Name is the name of the S3 to delete (required).
   293  	Name string
   294  }
   295  
   296  // DeleteS3 deletes the given S3 version.
   297  func (c *Client) DeleteS3(i *DeleteS3Input) error {
   298  	if i.ServiceID == "" {
   299  		return ErrMissingServiceID
   300  	}
   301  
   302  	if i.ServiceVersion == 0 {
   303  		return ErrMissingServiceVersion
   304  	}
   305  
   306  	if i.Name == "" {
   307  		return ErrMissingName
   308  	}
   309  
   310  	path := fmt.Sprintf("/service/%s/version/%d/logging/s3/%s", i.ServiceID, i.ServiceVersion, url.PathEscape(i.Name))
   311  	resp, err := c.Delete(path, nil)
   312  	if err != nil {
   313  		return err
   314  	}
   315  	defer resp.Body.Close()
   316  
   317  	var r *statusResp
   318  	if err := decodeBodyMap(resp.Body, &r); err != nil {
   319  		return err
   320  	}
   321  	if !r.Ok() {
   322  		return ErrNotOK
   323  	}
   324  	return nil
   325  }