Skip to content

Commit

Permalink
Adjust validations and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Didainius committed Jun 7, 2019
1 parent 6e81383 commit 5ba7ec4
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 24 deletions.
52 changes: 42 additions & 10 deletions vcd/resource_vcd_lb_service_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,21 @@ func ResourceVcdLbServiceMonitor() *schema.Resource {
Description: "Number of times the specified monitoring Method must fail sequentially before the server is declared down",
},
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
// ValidateFunc: validation.StringInSlice([]string{"http", "https", "tcp", "icmp", "udp"}, false),
Description: "Way in which you want to send the health check request to the server",
Type: schema.TypeString,
Required: true,
Description: "Way in which you want to send the health check request to the server",
ValidateFunc: validateLowerCase(),
},
"expected": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "String that the monitor expects to match in the status line of the HTTP or HTTPS response (for example, HTTP/1.1)",
},
"method": &schema.Schema{
Type: schema.TypeString,
Optional: true,
// ValidateFunc: validation.StringInSlice([]string{"GET", "OPTIONS", "POST"}, false),
Description: "Method to be used to detect server status",
Type: schema.TypeString,
Optional: true,
Description: "Method to be used to detect server status",
ValidateFunc: validateUpperCase(),
},
"url": &schema.Schema{
Type: schema.TypeString,
Expand All @@ -100,6 +100,40 @@ func ResourceVcdLbServiceMonitor() *schema.Resource {
}
}

// validateUpperCase checks that a string is upper cased
func validateUpperCase() schema.SchemaValidateFunc {
return func(i interface{}, k string) (s []string, es []error) {
v, ok := i.(string)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be string", k))
return
}

if strings.ToUpper(v) != v {
es = append(es, fmt.Errorf(
"expected string to be upper cased, got: %s", v))
}
return
}
}

// validateUpperCase checks that a string is upper cased
func validateLowerCase() schema.SchemaValidateFunc {
return func(i interface{}, k string) (s []string, es []error) {
v, ok := i.(string)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be string", k))
return
}

if strings.ToLower(v) != v {
es = append(es, fmt.Errorf(
"expected string to be lower cased, got: %s", v))
}
return
}
}

func resourceVcdLbServiceMonitorCreate(d *schema.ResourceData, meta interface{}) error {
vcdClient := meta.(*VCDClient)
vcdClient.lockParentEdgeGtw(d)
Expand All @@ -126,8 +160,6 @@ func resourceVcdLbServiceMonitorCreate(d *schema.ResourceData, meta interface{})

func resourceVcdLbServiceMonitorRead(d *schema.ResourceData, meta interface{}) error {
vcdClient := meta.(*VCDClient)
vcdClient.lockParentEdgeGtw(d)
defer vcdClient.unLockParentEdgeGtw(d)

edgeGateway, err := vcdClient.GetEdgeGatewayFromResource(d)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions vcd/resource_vcd_lb_service_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestAccVcdLbServiceMonitor(t *testing.T) {
"Interval": 5,
"Timeout": 10,
"MaxRetries": 3,
"Method": "ASD",
"Method": "POST",
"Tags": "lb lbServiceMonitor",
}

Expand All @@ -49,7 +49,7 @@ func TestAccVcdLbServiceMonitor(t *testing.T) {
Steps: []resource.TestStep{
resource.TestStep{
Config: configText,
Check: resource.ComposeTestCheckFunc(
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("vcd_lb_service_monitor.lb-service-monitor", "name", params["ServiceMonitorName"].(string)),
resource.TestMatchResourceAttr("vcd_lb_service_monitor.lb-service-monitor", "id", regexp.MustCompile(`^monitor-\d*$`)),
resource.TestCheckResourceAttr("vcd_lb_service_monitor.lb-service-monitor", "method", params["Method"].(string)),
Expand All @@ -70,7 +70,7 @@ func TestAccVcdLbServiceMonitor(t *testing.T) {
},
resource.TestStep{
Config: configTextStep1,
Check: resource.ComposeTestCheckFunc(
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("vcd_lb_service_monitor.lb-service-monitor", "name", params["ServiceMonitorName"].(string)),
resource.TestMatchResourceAttr("vcd_lb_service_monitor.lb-service-monitor", "id", regexp.MustCompile(`^monitor-\d*$`)),
resource.TestCheckResourceAttr("vcd_lb_service_monitor.lb-service-monitor", "type", "tcp"),
Expand Down
8 changes: 5 additions & 3 deletions vcd/test-templates/misc3.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Remove the first '#' from the next two lines to enable options for terraform executable
# apply-options -parallelism=1
# destroy-options -parallelism=1
## apply-options -parallelism=1
## destroy-options -parallelism=1

# Edge gateway load balancer configuration. It will get more details, but for now is used to
# make sure that locks work fine and it does not fail

# Edge gateway load balancer configuration
# v2.4.0+

variable "service_monitor_count" {
Expand Down
16 changes: 8 additions & 8 deletions website/docs/r/lb_service_monitor.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ resource "vcd_lb_service_monitor" "monitor" {
timeout = "20"
max_retries = "3"
type = "http"
http_method = "get"
http_method = "GET"
extension = {
content-type = "application/json"
linespan = ""
Expand All @@ -61,14 +61,14 @@ The following arguments are supported:
* `interval` - (Required) Interval at which a server is to be monitored using the specified Method.
* `timeout` - (Required) Maximum time in seconds within which a response from the server must be received
* `max_retries` - (Required) Number of times the specified monitoring Method must fail sequentially before the server is declared down
* `type` - (Required) Select the way in which you want to send the health check request to the server— `HTTP`, `HTTPS`,
`TCP`, `ICMP`, or `UDP`. Depending on the type selected, the remaining attributes are allowed or not
* `method` - (Optional) For types `HTTP` and `HTTPS`. Select the method to be used to detect server status
* `url` - (Optional) For types `HTTP` and `HTTPS`. URL to be used in the server status request
* `send` - (Optional) For types `HTTP`, `HTTPS`, and `UDP`. The data to be sent.
* `expected` - (Optional) For types `HTTP` and `HTTPS`. String that the monitor expects to match in the status line of
* `type` - (Required) Select the way in which you want to send the health check request to the server `http`, `https`,
`tcp`, `icmp`, or `udp`. Depending on the type selected, the remaining attributes are allowed or not
* `method` - (Optional) For types `http` and `https`. Select http method to be used to detect server status
* `url` - (Optional) For types `http` and `https`. URL to be used in the server status request
* `send` - (Optional) For types `http`, `https`, and `udp`. The data to be sent.
* `expected` - (Optional) For types `http` and `https`. String that the monitor expects to match in the status line of
the HTTP or HTTPS response (for example, `HTTP/1.1`)
* `receive` - (Optional) For types `HTTP`, `HTTPS`, and `UDP`. The string to be matched in the response content.
* `receive` - (Optional) For types `http`, `https`, and `udp`. The string to be matched in the response content.
**Note**: When `expected` is not matched, the monitor does not try to match the Receive content
* `extension` - (Required) A map of advanced monitor parameters as key=value pairs (i.e. `max-age=SECONDS`, `invert-regex`)
**Note**: When you need a value of `key` only format just set value to empty string (i.e. `linespan = ""`)
Expand Down

0 comments on commit 5ba7ec4

Please sign in to comment.