...

Source file src/github.com/Azure/azure-sdk-for-go/storage/storageservice.go

Documentation: github.com/Azure/azure-sdk-for-go/storage

     1  package storage
     2  
     3  // Copyright (c) Microsoft Corporation. All rights reserved.
     4  // Licensed under the MIT License. See License.txt in the project root for license information.
     5  
     6  import (
     7  	"net/http"
     8  	"net/url"
     9  	"strconv"
    10  )
    11  
    12  // ServiceProperties represents the storage account service properties
    13  type ServiceProperties struct {
    14  	Logging               *Logging
    15  	HourMetrics           *Metrics
    16  	MinuteMetrics         *Metrics
    17  	Cors                  *Cors
    18  	DeleteRetentionPolicy *RetentionPolicy // blob storage only
    19  	StaticWebsite         *StaticWebsite   // blob storage only
    20  }
    21  
    22  // Logging represents the Azure Analytics Logging settings
    23  type Logging struct {
    24  	Version         string
    25  	Delete          bool
    26  	Read            bool
    27  	Write           bool
    28  	RetentionPolicy *RetentionPolicy
    29  }
    30  
    31  // RetentionPolicy indicates if retention is enabled and for how many days
    32  type RetentionPolicy struct {
    33  	Enabled bool
    34  	Days    *int
    35  }
    36  
    37  // Metrics provide request statistics.
    38  type Metrics struct {
    39  	Version         string
    40  	Enabled         bool
    41  	IncludeAPIs     *bool
    42  	RetentionPolicy *RetentionPolicy
    43  }
    44  
    45  // Cors includes all the CORS rules
    46  type Cors struct {
    47  	CorsRule []CorsRule
    48  }
    49  
    50  // CorsRule includes all settings for a Cors rule
    51  type CorsRule struct {
    52  	AllowedOrigins  string
    53  	AllowedMethods  string
    54  	MaxAgeInSeconds int
    55  	ExposedHeaders  string
    56  	AllowedHeaders  string
    57  }
    58  
    59  // StaticWebsite - The properties that enable an account to host a static website
    60  type StaticWebsite struct {
    61  	// Enabled - Indicates whether this account is hosting a static website
    62  	Enabled bool
    63  	// IndexDocument - The default name of the index page under each directory
    64  	IndexDocument *string
    65  	// ErrorDocument404Path - The absolute path of the custom 404 page
    66  	ErrorDocument404Path *string
    67  }
    68  
    69  func (c Client) getServiceProperties(service string, auth authentication) (*ServiceProperties, error) {
    70  	query := url.Values{
    71  		"restype": {"service"},
    72  		"comp":    {"properties"},
    73  	}
    74  	uri := c.getEndpoint(service, "", query)
    75  	headers := c.getStandardHeaders()
    76  
    77  	resp, err := c.exec(http.MethodGet, uri, headers, nil, auth)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	defer resp.Body.Close()
    82  
    83  	if err := checkRespCode(resp, []int{http.StatusOK}); err != nil {
    84  		return nil, err
    85  	}
    86  
    87  	var out ServiceProperties
    88  	err = xmlUnmarshal(resp.Body, &out)
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  
    93  	return &out, nil
    94  }
    95  
    96  func (c Client) setServiceProperties(props ServiceProperties, service string, auth authentication) error {
    97  	query := url.Values{
    98  		"restype": {"service"},
    99  		"comp":    {"properties"},
   100  	}
   101  	uri := c.getEndpoint(service, "", query)
   102  
   103  	// Ideally, StorageServiceProperties would be the output struct
   104  	// This is to avoid golint stuttering, while generating the correct XML
   105  	type StorageServiceProperties struct {
   106  		Logging               *Logging
   107  		HourMetrics           *Metrics
   108  		MinuteMetrics         *Metrics
   109  		Cors                  *Cors
   110  		DeleteRetentionPolicy *RetentionPolicy
   111  		StaticWebsite         *StaticWebsite
   112  	}
   113  	input := StorageServiceProperties{
   114  		Logging:       props.Logging,
   115  		HourMetrics:   props.HourMetrics,
   116  		MinuteMetrics: props.MinuteMetrics,
   117  		Cors:          props.Cors,
   118  	}
   119  	// only set these fields for blob storage else it's invalid XML
   120  	if service == blobServiceName {
   121  		input.DeleteRetentionPolicy = props.DeleteRetentionPolicy
   122  		input.StaticWebsite = props.StaticWebsite
   123  	}
   124  
   125  	body, length, err := xmlMarshal(input)
   126  	if err != nil {
   127  		return err
   128  	}
   129  
   130  	headers := c.getStandardHeaders()
   131  	headers["Content-Length"] = strconv.Itoa(length)
   132  
   133  	resp, err := c.exec(http.MethodPut, uri, headers, body, auth)
   134  	if err != nil {
   135  		return err
   136  	}
   137  	defer drainRespBody(resp)
   138  	return checkRespCode(resp, []int{http.StatusAccepted})
   139  }
   140  

View as plain text