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

     1  package fastly
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  	gofastly "github.com/sethvargo/go-fastly"
    12  )
    13  
    14  func TestAccFastlyServiceV1_response_object_basic(t *testing.T) {
    15  	var service gofastly.ServiceDetail
    16  	name := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    17  	domainName1 := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10))
    18  
    19  	log1 := gofastly.ResponseObject{
    20  		Version:          1,
    21  		Name:             "responseObjecttesting",
    22  		Status:           200,
    23  		Response:         "OK",
    24  		Content:          "test content",
    25  		ContentType:      "text/html",
    26  		RequestCondition: "test-request-condition",
    27  		CacheCondition:   "test-cache-condition",
    28  	}
    29  
    30  	log2 := gofastly.ResponseObject{
    31  		Version:          1,
    32  		Name:             "responseObjecttesting2",
    33  		Status:           404,
    34  		Response:         "Not Found",
    35  		Content:          "some, other, content",
    36  		ContentType:      "text/csv",
    37  		RequestCondition: "another-test-request-condition",
    38  		CacheCondition:   "another-test-cache-condition",
    39  	}
    40  
    41  	resource.Test(t, resource.TestCase{
    42  		PreCheck:     func() { testAccPreCheck(t) },
    43  		Providers:    testAccProviders,
    44  		CheckDestroy: testAccCheckServiceV1Destroy,
    45  		Steps: []resource.TestStep{
    46  			resource.TestStep{
    47  				Config: testAccServiceV1ResponseObjectConfig(name, domainName1),
    48  				Check: resource.ComposeTestCheckFunc(
    49  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
    50  					testAccCheckFastlyServiceV1ResponseObjectAttributes(&service, []*gofastly.ResponseObject{&log1}),
    51  					resource.TestCheckResourceAttr(
    52  						"fastly_service_v1.foo", "name", name),
    53  					resource.TestCheckResourceAttr(
    54  						"fastly_service_v1.foo", "response_object.#", "1"),
    55  				),
    56  			},
    57  
    58  			resource.TestStep{
    59  				Config: testAccServiceV1ResponseObjectConfig_update(name, domainName1),
    60  				Check: resource.ComposeTestCheckFunc(
    61  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
    62  					testAccCheckFastlyServiceV1ResponseObjectAttributes(&service, []*gofastly.ResponseObject{&log1, &log2}),
    63  					resource.TestCheckResourceAttr(
    64  						"fastly_service_v1.foo", "name", name),
    65  					resource.TestCheckResourceAttr(
    66  						"fastly_service_v1.foo", "response_object.#", "2"),
    67  				),
    68  			},
    69  		},
    70  	})
    71  }
    72  
    73  func testAccCheckFastlyServiceV1ResponseObjectAttributes(service *gofastly.ServiceDetail, responseObjects []*gofastly.ResponseObject) resource.TestCheckFunc {
    74  	return func(s *terraform.State) error {
    75  
    76  		conn := testAccProvider.Meta().(*FastlyClient).conn
    77  		responseObjectList, err := conn.ListResponseObjects(&gofastly.ListResponseObjectsInput{
    78  			Service: service.ID,
    79  			Version: service.ActiveVersion.Number,
    80  		})
    81  
    82  		if err != nil {
    83  			return fmt.Errorf("[ERR] Error looking up Response Object for (%s), version (%s): %s", service.Name, service.ActiveVersion.Number, err)
    84  		}
    85  
    86  		if len(responseObjectList) != len(responseObjects) {
    87  			return fmt.Errorf("Response Object List count mismatch, expected (%d), got (%d)", len(responseObjects), len(responseObjectList))
    88  		}
    89  
    90  		var found int
    91  		for _, p := range responseObjects {
    92  			for _, lp := range responseObjectList {
    93  				if p.Name == lp.Name {
    94  					// we don't know these things ahead of time, so populate them now
    95  					p.ServiceID = service.ID
    96  					p.Version = service.ActiveVersion.Number
    97  					if !reflect.DeepEqual(p, lp) {
    98  						return fmt.Errorf("Bad match Response Object match, expected (%#v), got (%#v)", p, lp)
    99  					}
   100  					found++
   101  				}
   102  			}
   103  		}
   104  
   105  		if found != len(responseObjects) {
   106  			return fmt.Errorf("Error matching Response Object rules")
   107  		}
   108  
   109  		return nil
   110  	}
   111  }
   112  
   113  func testAccServiceV1ResponseObjectConfig(name, domain string) string {
   114  	return fmt.Sprintf(`
   115  resource "fastly_service_v1" "foo" {
   116    name = "%s"
   117  
   118    domain {
   119      name    = "%s"
   120      comment = "tf-testing-domain"
   121    }
   122  
   123    backend {
   124      address = "aws.amazon.com"
   125      name    = "amazon docs"
   126    }
   127  
   128  	condition {
   129      name      = "test-request-condition"
   130      type      = "REQUEST"
   131      priority  = 5
   132      statement = "req.url ~ \"^/foo/bar$\""
   133    }
   134  
   135  	condition {
   136      name      = "test-cache-condition"
   137      type      = "CACHE"
   138      priority  = 9
   139      statement = "req.url ~ \"^/articles/\""
   140    }
   141  
   142    response_object {
   143  		name              = "responseObjecttesting"
   144  		status            = 200
   145  		response          = "OK"
   146  		content           = "test content"
   147  		content_type      = "text/html"
   148  		request_condition = "test-request-condition"
   149  		cache_condition   = "test-cache-condition"
   150    }
   151  
   152    force_destroy = true
   153  }`, name, domain)
   154  }
   155  
   156  func testAccServiceV1ResponseObjectConfig_update(name, domain string) string {
   157  	return fmt.Sprintf(`
   158  resource "fastly_service_v1" "foo" {
   159    name = "%s"
   160  
   161    domain {
   162      name    = "%s"
   163      comment = "tf-testing-domain"
   164    }
   165  
   166    backend {
   167      address = "aws.amazon.com"
   168      name    = "amazon docs"
   169    }
   170  
   171  	condition {
   172      name      = "test-cache-condition"
   173      type      = "CACHE"
   174      priority  = 9
   175      statement = "req.url ~ \"^/articles/\""
   176    }
   177  
   178  	condition {
   179      name      = "another-test-cache-condition"
   180      type      = "CACHE"
   181      priority  = 7
   182      statement = "req.url ~ \"^/stories/\""
   183    }
   184  
   185  	condition {
   186      name      = "test-request-condition"
   187      type      = "REQUEST"
   188      priority  = 5
   189      statement = "req.url ~ \"^/foo/bar$\""
   190    }
   191  
   192  	condition {
   193      name      = "another-test-request-condition"
   194      type      = "REQUEST"
   195      priority  = 10
   196      statement = "req.url ~ \"^/articles$\""
   197    }
   198  
   199    response_object {
   200  		name              = "responseObjecttesting"
   201  		status            = 200
   202  		response          = "OK"
   203  		content           = "test content"
   204  		content_type      = "text/html"
   205  		request_condition = "test-request-condition"
   206  		cache_condition   = "test-cache-condition"
   207    }
   208  
   209    response_object {
   210  		name              = "responseObjecttesting2"
   211  		status            = 404
   212  		response          = "Not Found"
   213  		content           = "some, other, content"
   214  		content_type      = "text/csv"
   215  		request_condition = "another-test-request-condition"
   216  		cache_condition   = "another-test-cache-condition"
   217    }
   218  
   219    force_destroy = true
   220  }`, name, domain)
   221  }