github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/fastly/resource_fastly_service_v1_s3logging_test.go (about)

     1  package fastly
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/helper/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  	gofastly "github.com/sethvargo/go-fastly"
    13  )
    14  
    15  func TestAccFastlyServiceV1_s3logging_basic(t *testing.T) {
    16  	var service gofastly.ServiceDetail
    17  	name := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    18  	domainName1 := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10))
    19  
    20  	log1 := gofastly.S3{
    21  		Version:           1,
    22  		Name:              "somebucketlog",
    23  		BucketName:        "fastlytestlogging",
    24  		Domain:            "s3-us-west-2.amazonaws.com",
    25  		AccessKey:         "somekey",
    26  		SecretKey:         "somesecret",
    27  		Period:            uint(3600),
    28  		GzipLevel:         uint(0),
    29  		Format:            "%h %l %u %t %r %>s",
    30  		FormatVersion:     1,
    31  		TimestampFormat:   "%Y-%m-%dT%H:%M:%S.000",
    32  		ResponseCondition: "response_condition_test",
    33  	}
    34  
    35  	log2 := gofastly.S3{
    36  		Version:         1,
    37  		Name:            "someotherbucketlog",
    38  		BucketName:      "fastlytestlogging2",
    39  		Domain:          "s3-us-west-2.amazonaws.com",
    40  		AccessKey:       "someotherkey",
    41  		SecretKey:       "someothersecret",
    42  		GzipLevel:       uint(3),
    43  		Period:          uint(60),
    44  		Format:          "%h %l %u %t %r %>s",
    45  		FormatVersion:   1,
    46  		TimestampFormat: "%Y-%m-%dT%H:%M:%S.000",
    47  	}
    48  
    49  	resource.Test(t, resource.TestCase{
    50  		PreCheck:     func() { testAccPreCheck(t) },
    51  		Providers:    testAccProviders,
    52  		CheckDestroy: testAccCheckServiceV1Destroy,
    53  		Steps: []resource.TestStep{
    54  			{
    55  				Config: testAccServiceV1S3LoggingConfig(name, domainName1),
    56  				Check: resource.ComposeTestCheckFunc(
    57  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
    58  					testAccCheckFastlyServiceV1S3LoggingAttributes(&service, []*gofastly.S3{&log1}),
    59  					resource.TestCheckResourceAttr(
    60  						"fastly_service_v1.foo", "name", name),
    61  					resource.TestCheckResourceAttr(
    62  						"fastly_service_v1.foo", "s3logging.#", "1"),
    63  				),
    64  			},
    65  
    66  			{
    67  				Config: testAccServiceV1S3LoggingConfig_update(name, domainName1),
    68  				Check: resource.ComposeTestCheckFunc(
    69  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
    70  					testAccCheckFastlyServiceV1S3LoggingAttributes(&service, []*gofastly.S3{&log1, &log2}),
    71  					resource.TestCheckResourceAttr(
    72  						"fastly_service_v1.foo", "name", name),
    73  					resource.TestCheckResourceAttr(
    74  						"fastly_service_v1.foo", "s3logging.#", "2"),
    75  				),
    76  			},
    77  		},
    78  	})
    79  }
    80  
    81  // Tests that s3_access_key and s3_secret_key are read from the env
    82  func TestAccFastlyServiceV1_s3logging_s3_env(t *testing.T) {
    83  	var service gofastly.ServiceDetail
    84  	name := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    85  	domainName1 := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10))
    86  
    87  	// set env Vars to something we expect
    88  	resetEnv := setEnv("someEnv", t)
    89  	defer resetEnv()
    90  
    91  	log3 := gofastly.S3{
    92  		Version:         1,
    93  		Name:            "somebucketlog",
    94  		BucketName:      "fastlytestlogging",
    95  		Domain:          "s3-us-west-2.amazonaws.com",
    96  		AccessKey:       "someEnv",
    97  		SecretKey:       "someEnv",
    98  		Period:          uint(3600),
    99  		GzipLevel:       uint(0),
   100  		Format:          "%h %l %u %t %r %>s",
   101  		FormatVersion:   1,
   102  		TimestampFormat: "%Y-%m-%dT%H:%M:%S.000",
   103  	}
   104  
   105  	resource.Test(t, resource.TestCase{
   106  		PreCheck:     func() { testAccPreCheck(t) },
   107  		Providers:    testAccProviders,
   108  		CheckDestroy: testAccCheckServiceV1Destroy,
   109  		Steps: []resource.TestStep{
   110  			{
   111  				Config: testAccServiceV1S3LoggingConfig_env(name, domainName1),
   112  				Check: resource.ComposeTestCheckFunc(
   113  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
   114  					testAccCheckFastlyServiceV1S3LoggingAttributes(&service, []*gofastly.S3{&log3}),
   115  					resource.TestCheckResourceAttr(
   116  						"fastly_service_v1.foo", "name", name),
   117  					resource.TestCheckResourceAttr(
   118  						"fastly_service_v1.foo", "s3logging.#", "1"),
   119  				),
   120  			},
   121  		},
   122  	})
   123  }
   124  
   125  func TestAccFastlyServiceV1_s3logging_formatVersion(t *testing.T) {
   126  	var service gofastly.ServiceDetail
   127  	name := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
   128  	domainName1 := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10))
   129  
   130  	log1 := gofastly.S3{
   131  		Version:         1,
   132  		Name:            "somebucketlog",
   133  		BucketName:      "fastlytestlogging",
   134  		Domain:          "s3-us-west-2.amazonaws.com",
   135  		AccessKey:       "somekey",
   136  		SecretKey:       "somesecret",
   137  		Period:          uint(3600),
   138  		GzipLevel:       uint(0),
   139  		Format:          "%a %l %u %t %m %U%q %H %>s %b %T",
   140  		FormatVersion:   2,
   141  		TimestampFormat: "%Y-%m-%dT%H:%M:%S.000",
   142  	}
   143  
   144  	resource.Test(t, resource.TestCase{
   145  		PreCheck:     func() { testAccPreCheck(t) },
   146  		Providers:    testAccProviders,
   147  		CheckDestroy: testAccCheckServiceV1Destroy,
   148  		Steps: []resource.TestStep{
   149  			{
   150  				Config: testAccServiceV1S3LoggingConfig_formatVersion(name, domainName1),
   151  				Check: resource.ComposeTestCheckFunc(
   152  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
   153  					testAccCheckFastlyServiceV1S3LoggingAttributes(&service, []*gofastly.S3{&log1}),
   154  					resource.TestCheckResourceAttr(
   155  						"fastly_service_v1.foo", "name", name),
   156  					resource.TestCheckResourceAttr(
   157  						"fastly_service_v1.foo", "s3logging.#", "1"),
   158  				),
   159  			},
   160  		},
   161  	})
   162  }
   163  
   164  func testAccCheckFastlyServiceV1S3LoggingAttributes(service *gofastly.ServiceDetail, s3s []*gofastly.S3) resource.TestCheckFunc {
   165  	return func(s *terraform.State) error {
   166  
   167  		conn := testAccProvider.Meta().(*FastlyClient).conn
   168  		s3List, err := conn.ListS3s(&gofastly.ListS3sInput{
   169  			Service: service.ID,
   170  			Version: service.ActiveVersion.Number,
   171  		})
   172  
   173  		if err != nil {
   174  			return fmt.Errorf("[ERR] Error looking up S3 Logging for (%s), version (%s): %s", service.Name, service.ActiveVersion.Number, err)
   175  		}
   176  
   177  		if len(s3List) != len(s3s) {
   178  			return fmt.Errorf("S3 List count mismatch, expected (%d), got (%d)", len(s3s), len(s3List))
   179  		}
   180  
   181  		var found int
   182  		for _, s := range s3s {
   183  			for _, ls := range s3List {
   184  				if s.Name == ls.Name {
   185  					// we don't know these things ahead of time, so populate them now
   186  					s.ServiceID = service.ID
   187  					s.Version = service.ActiveVersion.Number
   188  					// We don't track these, so clear them out because we also wont know
   189  					// these ahead of time
   190  					ls.CreatedAt = nil
   191  					ls.UpdatedAt = nil
   192  					if !reflect.DeepEqual(s, ls) {
   193  						return fmt.Errorf("Bad match S3 logging match, expected (%#v), got (%#v)", s, ls)
   194  					}
   195  					found++
   196  				}
   197  			}
   198  		}
   199  
   200  		if found != len(s3s) {
   201  			return fmt.Errorf("Error matching S3 Logging rules")
   202  		}
   203  
   204  		return nil
   205  	}
   206  }
   207  
   208  func testAccServiceV1S3LoggingConfig(name, domain string) string {
   209  	return fmt.Sprintf(`
   210  resource "fastly_service_v1" "foo" {
   211    name = "%s"
   212  
   213    domain {
   214      name    = "%s"
   215      comment = "tf-testing-domain"
   216    }
   217  
   218    backend {
   219      address = "aws.amazon.com"
   220      name    = "amazon docs"
   221    }
   222  
   223  	condition {
   224      name      = "response_condition_test"
   225      type      = "RESPONSE"
   226      priority  = 8
   227      statement = "resp.status == 418"
   228    }
   229  
   230    s3logging {
   231      name               = "somebucketlog"
   232      bucket_name        = "fastlytestlogging"
   233      domain             = "s3-us-west-2.amazonaws.com"
   234      s3_access_key      = "somekey"
   235      s3_secret_key      = "somesecret"
   236  		response_condition = "response_condition_test"
   237    }
   238  
   239    force_destroy = true
   240  }`, name, domain)
   241  }
   242  
   243  func testAccServiceV1S3LoggingConfig_update(name, domain string) string {
   244  	return fmt.Sprintf(`
   245  resource "fastly_service_v1" "foo" {
   246    name = "%s"
   247  
   248    domain {
   249      name    = "%s"
   250      comment = "tf-testing-domain"
   251    }
   252  
   253    backend {
   254      address = "aws.amazon.com"
   255      name    = "amazon docs"
   256    }
   257  
   258  	condition {
   259      name      = "response_condition_test"
   260      type      = "RESPONSE"
   261      priority  = 8
   262      statement = "resp.status == 418"
   263    }
   264  
   265    s3logging {
   266      name               = "somebucketlog"
   267      bucket_name        = "fastlytestlogging"
   268      domain             = "s3-us-west-2.amazonaws.com"
   269      s3_access_key      = "somekey"
   270      s3_secret_key      = "somesecret"
   271  		response_condition = "response_condition_test"
   272    }
   273  
   274    s3logging {
   275      name          = "someotherbucketlog"
   276      bucket_name   = "fastlytestlogging2"
   277      domain        = "s3-us-west-2.amazonaws.com"
   278      s3_access_key = "someotherkey"
   279      s3_secret_key = "someothersecret"
   280      period        = 60
   281      gzip_level    = 3
   282    }
   283  
   284    force_destroy = true
   285  }`, name, domain)
   286  }
   287  
   288  func testAccServiceV1S3LoggingConfig_env(name, domain string) string {
   289  	return fmt.Sprintf(`
   290  resource "fastly_service_v1" "foo" {
   291    name = "%s"
   292  
   293    domain {
   294      name    = "%s"
   295      comment = "tf-testing-domain"
   296    }
   297  
   298    backend {
   299      address = "aws.amazon.com"
   300      name    = "amazon docs"
   301    }
   302  
   303    s3logging {
   304      name          = "somebucketlog"
   305      bucket_name   = "fastlytestlogging"
   306      domain        = "s3-us-west-2.amazonaws.com"
   307    }
   308  
   309    force_destroy = true
   310  }`, name, domain)
   311  }
   312  
   313  func testAccServiceV1S3LoggingConfig_formatVersion(name, domain string) string {
   314  	return fmt.Sprintf(`
   315  resource "fastly_service_v1" "foo" {
   316    name = "%s"
   317  
   318    domain {
   319      name    = "%s"
   320      comment = "tf-testing-domain"
   321    }
   322  
   323    backend {
   324      address = "aws.amazon.com"
   325      name    = "amazon docs"
   326    }
   327  
   328    s3logging {
   329      name           = "somebucketlog"
   330      bucket_name    = "fastlytestlogging"
   331      domain         = "s3-us-west-2.amazonaws.com"
   332      s3_access_key  = "somekey"
   333      s3_secret_key  = "somesecret"
   334      format         = "%%a %%l %%u %%t %%m %%U%%q %%H %%>s %%b %%T"
   335      format_version = 2
   336    }
   337  
   338    force_destroy = true
   339  }`, name, domain)
   340  }
   341  
   342  func setEnv(s string, t *testing.T) func() {
   343  	e := getEnv()
   344  	// Set all the envs to a dummy value
   345  	if err := os.Setenv("FASTLY_S3_ACCESS_KEY", s); err != nil {
   346  		t.Fatalf("Error setting env var AWS_ACCESS_KEY_ID: %s", err)
   347  	}
   348  	if err := os.Setenv("FASTLY_S3_SECRET_KEY", s); err != nil {
   349  		t.Fatalf("Error setting env var FASTLY_S3_SECRET_KEY: %s", err)
   350  	}
   351  
   352  	return func() {
   353  		// re-set all the envs we unset above
   354  		if err := os.Setenv("FASTLY_S3_ACCESS_KEY", e.Key); err != nil {
   355  			t.Fatalf("Error resetting env var AWS_ACCESS_KEY_ID: %s", err)
   356  		}
   357  		if err := os.Setenv("FASTLY_S3_SECRET_KEY", e.Secret); err != nil {
   358  			t.Fatalf("Error resetting env var FASTLY_S3_SECRET_KEY: %s", err)
   359  		}
   360  	}
   361  }
   362  
   363  // struct to preserve the current environment
   364  type currentEnv struct {
   365  	Key, Secret string
   366  }
   367  
   368  func getEnv() *currentEnv {
   369  	// Grab any existing Fastly AWS S3 keys and preserve, in the off chance
   370  	// they're actually set in the enviornment
   371  	return &currentEnv{
   372  		Key:    os.Getenv("FASTLY_S3_ACCESS_KEY"),
   373  		Secret: os.Getenv("FASTLY_S3_SECRET_KEY"),
   374  	}
   375  }