github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/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  		TimestampFormat: "%Y-%m-%dT%H:%M:%S.000",
    31  	}
    32  
    33  	log2 := gofastly.S3{
    34  		Version:         "1",
    35  		Name:            "someotherbucketlog",
    36  		BucketName:      "fastlytestlogging2",
    37  		Domain:          "s3-us-west-2.amazonaws.com",
    38  		AccessKey:       "someotherkey",
    39  		SecretKey:       "someothersecret",
    40  		GzipLevel:       uint(3),
    41  		Period:          uint(60),
    42  		Format:          "%h %l %u %t %r %>s",
    43  		TimestampFormat: "%Y-%m-%dT%H:%M:%S.000",
    44  	}
    45  
    46  	resource.Test(t, resource.TestCase{
    47  		PreCheck:     func() { testAccPreCheck(t) },
    48  		Providers:    testAccProviders,
    49  		CheckDestroy: testAccCheckServiceV1Destroy,
    50  		Steps: []resource.TestStep{
    51  			resource.TestStep{
    52  				Config: testAccServiceV1S3LoggingConfig(name, domainName1),
    53  				Check: resource.ComposeTestCheckFunc(
    54  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
    55  					testAccCheckFastlyServiceV1S3LoggingAttributes(&service, []*gofastly.S3{&log1}),
    56  					resource.TestCheckResourceAttr(
    57  						"fastly_service_v1.foo", "name", name),
    58  					resource.TestCheckResourceAttr(
    59  						"fastly_service_v1.foo", "s3logging.#", "1"),
    60  				),
    61  			},
    62  
    63  			resource.TestStep{
    64  				Config: testAccServiceV1S3LoggingConfig_update(name, domainName1),
    65  				Check: resource.ComposeTestCheckFunc(
    66  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
    67  					testAccCheckFastlyServiceV1S3LoggingAttributes(&service, []*gofastly.S3{&log1, &log2}),
    68  					resource.TestCheckResourceAttr(
    69  						"fastly_service_v1.foo", "name", name),
    70  					resource.TestCheckResourceAttr(
    71  						"fastly_service_v1.foo", "s3logging.#", "2"),
    72  				),
    73  			},
    74  		},
    75  	})
    76  }
    77  
    78  // Tests that s3_access_key and s3_secret_key are read from the env
    79  func TestAccFastlyServiceV1_s3logging_s3_env(t *testing.T) {
    80  	var service gofastly.ServiceDetail
    81  	name := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    82  	domainName1 := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10))
    83  
    84  	// set env Vars to something we expect
    85  	resetEnv := setEnv("someEnv", t)
    86  	defer resetEnv()
    87  
    88  	log3 := gofastly.S3{
    89  		Version:         "1",
    90  		Name:            "somebucketlog",
    91  		BucketName:      "fastlytestlogging",
    92  		Domain:          "s3-us-west-2.amazonaws.com",
    93  		AccessKey:       "someEnv",
    94  		SecretKey:       "someEnv",
    95  		Period:          uint(3600),
    96  		GzipLevel:       uint(0),
    97  		Format:          "%h %l %u %t %r %>s",
    98  		TimestampFormat: "%Y-%m-%dT%H:%M:%S.000",
    99  	}
   100  
   101  	resource.Test(t, resource.TestCase{
   102  		PreCheck:     func() { testAccPreCheck(t) },
   103  		Providers:    testAccProviders,
   104  		CheckDestroy: testAccCheckServiceV1Destroy,
   105  		Steps: []resource.TestStep{
   106  			resource.TestStep{
   107  				Config: testAccServiceV1S3LoggingConfig_env(name, domainName1),
   108  				Check: resource.ComposeTestCheckFunc(
   109  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
   110  					testAccCheckFastlyServiceV1S3LoggingAttributes(&service, []*gofastly.S3{&log3}),
   111  					resource.TestCheckResourceAttr(
   112  						"fastly_service_v1.foo", "name", name),
   113  					resource.TestCheckResourceAttr(
   114  						"fastly_service_v1.foo", "s3logging.#", "1"),
   115  				),
   116  			},
   117  		},
   118  	})
   119  }
   120  
   121  func testAccCheckFastlyServiceV1S3LoggingAttributes(service *gofastly.ServiceDetail, s3s []*gofastly.S3) resource.TestCheckFunc {
   122  	return func(s *terraform.State) error {
   123  
   124  		conn := testAccProvider.Meta().(*FastlyClient).conn
   125  		s3List, err := conn.ListS3s(&gofastly.ListS3sInput{
   126  			Service: service.ID,
   127  			Version: service.ActiveVersion.Number,
   128  		})
   129  
   130  		if err != nil {
   131  			return fmt.Errorf("[ERR] Error looking up S3 Logging for (%s), version (%s): %s", service.Name, service.ActiveVersion.Number, err)
   132  		}
   133  
   134  		if len(s3List) != len(s3s) {
   135  			return fmt.Errorf("S3 List count mismatch, expected (%d), got (%d)", len(s3s), len(s3List))
   136  		}
   137  
   138  		var found int
   139  		for _, s := range s3s {
   140  			for _, ls := range s3List {
   141  				if s.Name == ls.Name {
   142  					// we don't know these things ahead of time, so populate them now
   143  					s.ServiceID = service.ID
   144  					s.Version = service.ActiveVersion.Number
   145  					// We don't track these, so clear them out because we also wont know
   146  					// these ahead of time
   147  					ls.CreatedAt = nil
   148  					ls.UpdatedAt = nil
   149  					if !reflect.DeepEqual(s, ls) {
   150  						return fmt.Errorf("Bad match S3 logging match, expected (%#v), got (%#v)", s, ls)
   151  					}
   152  					found++
   153  				}
   154  			}
   155  		}
   156  
   157  		if found != len(s3s) {
   158  			return fmt.Errorf("Error matching S3 Logging rules")
   159  		}
   160  
   161  		return nil
   162  	}
   163  }
   164  
   165  func testAccServiceV1S3LoggingConfig(name, domain string) string {
   166  	return fmt.Sprintf(`
   167  resource "fastly_service_v1" "foo" {
   168    name = "%s"
   169  
   170    domain {
   171      name    = "%s"
   172      comment = "tf-testing-domain"
   173    }
   174  
   175    backend {
   176      address = "aws.amazon.com"
   177      name    = "amazon docs"
   178    }
   179  
   180    s3logging {
   181      name       = "somebucketlog"
   182                  bucket_name = "fastlytestlogging"
   183      domain     = "s3-us-west-2.amazonaws.com"
   184                  s3_access_key = "somekey"
   185      s3_secret_key = "somesecret"
   186    }
   187  
   188    force_destroy = true
   189  }`, name, domain)
   190  }
   191  
   192  func testAccServiceV1S3LoggingConfig_update(name, domain string) string {
   193  	return fmt.Sprintf(`
   194  resource "fastly_service_v1" "foo" {
   195    name = "%s"
   196  
   197    domain {
   198      name    = "%s"
   199      comment = "tf-testing-domain"
   200    }
   201  
   202    backend {
   203      address = "aws.amazon.com"
   204      name    = "amazon docs"
   205    }
   206  
   207    s3logging {
   208      name          = "somebucketlog"
   209      bucket_name   = "fastlytestlogging"
   210      domain        = "s3-us-west-2.amazonaws.com"
   211      s3_access_key = "somekey"
   212      s3_secret_key = "somesecret"
   213    }
   214  
   215    s3logging {
   216      name          = "someotherbucketlog"
   217      bucket_name   = "fastlytestlogging2"
   218      domain        = "s3-us-west-2.amazonaws.com"
   219      s3_access_key = "someotherkey"
   220      s3_secret_key = "someothersecret"
   221      period        = 60
   222      gzip_level    = 3
   223    }
   224  
   225    force_destroy = true
   226  }`, name, domain)
   227  }
   228  
   229  func testAccServiceV1S3LoggingConfig_env(name, domain string) string {
   230  	return fmt.Sprintf(`
   231  resource "fastly_service_v1" "foo" {
   232    name = "%s"
   233  
   234    domain {
   235      name    = "%s"
   236      comment = "tf-testing-domain"
   237    }
   238  
   239    backend {
   240      address = "aws.amazon.com"
   241      name    = "amazon docs"
   242    }
   243  
   244    s3logging {
   245      name          = "somebucketlog"
   246      bucket_name   = "fastlytestlogging"
   247      domain        = "s3-us-west-2.amazonaws.com"
   248    }
   249  
   250    force_destroy = true
   251  }`, name, domain)
   252  }
   253  
   254  func setEnv(s string, t *testing.T) func() {
   255  	e := getEnv()
   256  	// Set all the envs to a dummy value
   257  	if err := os.Setenv("FASTLY_S3_ACCESS_KEY", s); err != nil {
   258  		t.Fatalf("Error setting env var AWS_ACCESS_KEY_ID: %s", err)
   259  	}
   260  	if err := os.Setenv("FASTLY_S3_SECRET_KEY", s); err != nil {
   261  		t.Fatalf("Error setting env var FASTLY_S3_SECRET_KEY: %s", err)
   262  	}
   263  
   264  	return func() {
   265  		// re-set all the envs we unset above
   266  		if err := os.Setenv("FASTLY_S3_ACCESS_KEY", e.Key); err != nil {
   267  			t.Fatalf("Error resetting env var AWS_ACCESS_KEY_ID: %s", err)
   268  		}
   269  		if err := os.Setenv("FASTLY_S3_SECRET_KEY", e.Secret); err != nil {
   270  			t.Fatalf("Error resetting env var FASTLY_S3_SECRET_KEY: %s", err)
   271  		}
   272  	}
   273  }
   274  
   275  // struct to preserve the current environment
   276  type currentEnv struct {
   277  	Key, Secret string
   278  }
   279  
   280  func getEnv() *currentEnv {
   281  	// Grab any existing Fastly AWS S3 keys and preserve, in the off chance
   282  	// they're actually set in the enviornment
   283  	return &currentEnv{
   284  		Key:    os.Getenv("FASTLY_S3_ACCESS_KEY"),
   285  		Secret: os.Getenv("FASTLY_S3_SECRET_KEY"),
   286  	}
   287  }